Commit d5c97607 authored by Geoff Simmons's avatar Geoff Simmons

add the error() method

parent 78bdc8b7
...@@ -46,6 +46,10 @@ varnish v1 -vcl+backend { ...@@ -46,6 +46,10 @@ varnish v1 -vcl+backend {
} else { } else {
set beresp.status = 999; set beresp.status = 999;
} }
set beresp.http.foobarerr = foobar.error();
set beresp.http.snafuerr = snafu.error();
set beresp.http.barerr = bar.error();
} }
} -start } -start
...@@ -56,4 +60,8 @@ client c1 { ...@@ -56,4 +60,8 @@ client c1 {
expect resp.status == "200" expect resp.status == "200"
expect resp.http.foo1 == "1" expect resp.http.foo1 == "1"
expect resp.http.bar1 == "2" expect resp.http.bar1 == "2"
expect resp.http.foobarerr == ""
expect resp.http.snafuerr == ""
expect resp.http.barerr == ""
} -run } -run
...@@ -22,6 +22,9 @@ varnish v1 -vcl+backend { ...@@ -22,6 +22,9 @@ varnish v1 -vcl+backend {
if (paren.match(beresp.http.foo)) { if (paren.match(beresp.http.foo)) {
set beresp.http.foo = "baz"; set beresp.http.foo = "baz";
} }
else {
set beresp.http.error = paren.error();
}
} }
} -start } -start
...@@ -32,6 +35,8 @@ client c1 { ...@@ -32,6 +35,8 @@ client c1 {
expect resp.http.content-length == 6 expect resp.http.content-length == 6
expect resp.http.foo == "bar" expect resp.http.foo == "bar"
expect resp.http.baz == "quux" expect resp.http.baz == "quux"
expect resp.http.error != <undef>
expect resp.http.error != ""
} -run } -run
logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" { logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
......
...@@ -15,6 +15,7 @@ varnish v1 -vcl+backend { ...@@ -15,6 +15,7 @@ varnish v1 -vcl+backend {
sub vcl_deliver { sub vcl_deliver {
set resp.http.paren = paren.failed(); set resp.http.paren = paren.failed();
set resp.http.error = paren.error();
/* match fails */ /* match fails */
if (paren.match(resp.http.foo)) { if (paren.match(resp.http.foo)) {
...@@ -34,8 +35,9 @@ varnish v1 -vcl+backend { ...@@ -34,8 +35,9 @@ varnish v1 -vcl+backend {
set resp.http.paren10 = paren.backref(10, "fallback10"); set resp.http.paren10 = paren.backref(10, "fallback10");
if (paren.match_dyn("(bar)", resp.http.foo)) { if (paren.match_dyn("(bar)", resp.http.foo)) {
# dynamic matches do not affect failed() # dynamic matches do not affect failed() or error()
set resp.http.parend = paren.failed(); set resp.http.parend = paren.failed();
set resp.http.errord = paren.error();
} }
/* compilation fails */ /* compilation fails */
...@@ -63,6 +65,8 @@ client c1 { ...@@ -63,6 +65,8 @@ client c1 {
rxresp rxresp
expect resp.status == 200 expect resp.status == 200
expect resp.http.paren == "true" expect resp.http.paren == "true"
expect resp.http.error != <undef>
expect resp.http.error != ""
expect resp.http.match == <undef> expect resp.http.match == <undef>
expect resp.http.paren0 == "fallback0" expect resp.http.paren0 == "fallback0"
expect resp.http.paren1 == "fallback1" expect resp.http.paren1 == "fallback1"
...@@ -78,6 +82,8 @@ client c1 { ...@@ -78,6 +82,8 @@ client c1 {
expect resp.http.parend == "true" expect resp.http.parend == "true"
expect resp.http.matchd == <undef> expect resp.http.matchd == <undef>
expect resp.http.errord != <undef>
expect resp.http.errord != ""
expect resp.http.paren0d == "fallback0" expect resp.http.paren0d == "fallback0"
expect resp.http.paren1d == "fallback1" expect resp.http.paren1d == "fallback1"
expect resp.http.paren2d == "fallback2" expect resp.http.paren2d == "fallback2"
......
...@@ -115,6 +115,25 @@ vmod_regex_failed(const struct vrt_ctx *ctx, struct vmod_re_regex *re) ...@@ -115,6 +115,25 @@ vmod_regex_failed(const struct vrt_ctx *ctx, struct vmod_re_regex *re)
return (re->error != NULL); return (re->error != NULL);
} }
VCL_STRING
vmod_regex_error(const struct vrt_ctx *ctx, struct vmod_re_regex *re)
{
VCL_STRING error;
CHECK_OBJ_NOTNULL(re, VMOD_RE_REGEX_MAGIC);
if (re->error == NULL)
return "";
error = (VCL_STRING) WS_Printf(ctx->ws, "%s (position %d)", re->error,
re->erroffset);
if (error == NULL) {
VSLb(ctx->vsl, SLT_VCL_Error,
"vmod re: insufficient workspace for error message");
return "insufficient workspace for error message";
}
return(error);
}
VCL_VOID VCL_VOID
vmod_regex__fini(struct vmod_re_regex **rep) vmod_regex__fini(struct vmod_re_regex **rep)
{ {
......
...@@ -121,10 +121,23 @@ $Method BOOL .failed() ...@@ -121,10 +121,23 @@ $Method BOOL .failed()
Description Description
Returns true if regex compilation in the constructor failed. Returns true if regex compilation in the constructor failed.
NOTE: This method only pertains to compilation in the
constructor, not to compilation of a regex in ``match_dyn``.
Example Example
``if (myregex.failed()) { # ...`` ``if (myregex.failed()) { # ...``
$Method STRING .error()
Description
Returns an error message if regex compilation in the
constructor failed, or the empty string if compilation
succedded. Like ``failed`` this only pertains to failures of
compilation in the constructor, not in ``match_dyn``.
Example
``std.log("myregex failed to compile: " + myregex.error());``
$Method BOOL .match(STRING) $Method BOOL .match(STRING)
Description Description
...@@ -262,8 +275,9 @@ LIMITATIONS ...@@ -262,8 +275,9 @@ LIMITATIONS
Regular expressions passed into the constructor and into ``match_dyn`` Regular expressions passed into the constructor and into ``match_dyn``
are compiled at run-time, so there are no errors at VCL compile-time are compiled at run-time, so there are no errors at VCL compile-time
for invalid expressions. If an expression is invalid, then a for invalid expressions. If an expression is invalid, then a
``VCL_error`` message is emitted to the Varnish log, and matches ``VCL_error`` message is emitted to the Varnish log, matches always
always fail. fail, and errors in the constructor can be inspected with ``failed``
and ``error``.
The VMOD allocates memory for captured subexpressions from Varnish The VMOD allocates memory for captured subexpressions from Varnish
workspaces, whose sizes are determined by the runtime parameters workspaces, whose sizes are determined by the runtime parameters
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment