}
if (i == n) {
- njs_vm_error(ctx->engine->u.njs.vm, "unknown event \"%V\"", event);
+ njs_vm_type_error(ctx->engine->u.njs.vm, "unknown event \"%V\"",
+ event);
return NULL;
}
for (n = 0; n < NGX_JS_EVENT_MAX; n++) {
type = ctx->events[n].data_type;
if (type != NGX_JS_UNSET && type != events[i].data_type) {
- njs_vm_error(ctx->engine->u.njs.vm, "mixing string and buffer"
- " events is not allowed");
+ njs_vm_type_error(ctx->engine->u.njs.vm, "mixing string and buffer"
+ " events is not allowed");
return NULL;
}
}
s = njs_vm_external(vm, ngx_stream_js_session_proto_id,
njs_argument(args, 0));
if (s == NULL) {
- njs_vm_error(vm, "\"this\" is not an external");
+ njs_vm_type_error(vm, "\"this\" is not an external");
return NJS_ERROR;
}
}
if (status < NGX_ABORT || status > NGX_STREAM_SERVICE_UNAVAILABLE) {
- njs_vm_error(vm, "code is out of range");
+ njs_vm_range_error(vm, "code is out of range");
return NJS_ERROR;
}
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
if (ctx->filter) {
- njs_vm_error(vm, "should not be called while filtering");
+ njs_vm_type_error(vm, "should not be called while filtering");
return NJS_ERROR;
}
s = njs_vm_external(vm, ngx_stream_js_session_proto_id,
njs_argument(args, 0));
if (s == NULL) {
- njs_vm_error(vm, "\"this\" is not an external");
+ njs_vm_type_error(vm, "\"this\" is not an external");
return NJS_ERROR;
}
if (ngx_js_string(vm, njs_arg(args, nargs, 1), &name) == NJS_ERROR) {
- njs_vm_error(vm, "failed to convert event arg");
+ njs_vm_type_error(vm, "failed to convert event arg");
return NJS_ERROR;
}
callback = njs_arg(args, nargs, 2);
if (!njs_value_is_function(callback)) {
- njs_vm_error(vm, "callback is not a function");
+ njs_vm_type_error(vm, "callback is not a function");
return NJS_ERROR;
}
}
if (njs_value_is_function(njs_value_arg(&event->function))) {
- njs_vm_error(vm, "event handler \"%V\" is already set", &name);
+ njs_vm_type_error(vm, "event handler \"%V\" is already set", &name);
return NJS_ERROR;
}
s = njs_vm_external(vm, ngx_stream_js_session_proto_id,
njs_argument(args, 0));
if (s == NULL) {
- njs_vm_error(vm, "\"this\" is not an external");
+ njs_vm_type_error(vm, "\"this\" is not an external");
return NJS_ERROR;
}
if (ngx_js_string(vm, njs_arg(args, nargs, 1), &name) == NJS_ERROR) {
- njs_vm_error(vm, "failed to convert event arg");
+ njs_vm_type_error(vm, "failed to convert event arg");
return NJS_ERROR;
}
s = njs_vm_external(vm, ngx_stream_js_session_proto_id,
njs_argument(args, 0));
if (s == NULL) {
- njs_vm_error(vm, "\"this\" is not an external");
+ njs_vm_type_error(vm, "\"this\" is not an external");
return NJS_ERROR;
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
if (!ctx->filter) {
- njs_vm_error(vm, "cannot send buffer in this handler");
+ njs_vm_type_error(vm, "cannot send buffer in this handler");
return NJS_ERROR;
}
if (ngx_js_string(vm, njs_arg(args, nargs, 1), &buffer) != NGX_OK) {
- njs_vm_error(vm, "failed to get buffer arg");
+ njs_vm_type_error(vm, "failed to get buffer arg");
return NJS_ERROR;
}
cl = ngx_chain_get_free_buf(c->pool, &ctx->free);
if (cl == NULL) {
- njs_vm_error(vm, "memory error");
+ njs_vm_memory_error(vm);
return NJS_ERROR;
}
} else {
if (ngx_stream_js_next_filter(s, ctx, cl, from_upstream) == NGX_ERROR) {
- njs_vm_error(vm, "ngx_stream_js_next_filter() failed");
+ njs_vm_internal_error(vm, "ngx_stream_js_next_filter() failed");
return NJS_ERROR;
}
}
exception:
- njs_vm_error(vm, "\"from_upstream\" flag is expected when"
- "called asynchronously");
+ njs_vm_type_error(vm, "\"from_upstream\" flag is expected when "
+ "called asynchronously");
return NJS_ERROR;
}
s = njs_vm_external(vm, ngx_stream_js_session_proto_id,
njs_argument(args, 0));
if (s == NULL) {
- njs_vm_error(vm, "\"this\" is not an external");
+ njs_vm_type_error(vm, "\"this\" is not an external");
return NJS_ERROR;
}
s = njs_vm_external(vm, ngx_stream_js_session_proto_id,
njs_argument(args, 0));
if (s == NULL) {
- njs_vm_error(vm, "\"this\" is not an external");
+ njs_vm_type_error(vm, "\"this\" is not an external");
return NJS_ERROR;
}
} else {
name.data = ngx_pnalloc(s->connection->pool, val.length);
if (name.data == NULL) {
- njs_vm_error(vm, "internal error");
+ njs_vm_memory_error(vm);
return NJS_ERROR;
}
}
} else {
name.data = ngx_pnalloc(s->connection->pool, val.length);
if (name.data == NULL) {
- njs_vm_error(vm, "internal error");
+ njs_vm_memory_error(vm);
return NJS_ERROR;
}
}
v = ngx_hash_find(&cmcf->variables_hash, key, name.data, val.length);
if (v == NULL) {
- njs_vm_error(vm, "variable not found");
+ njs_vm_type_error(vm, "variable not found");
return NJS_ERROR;
}
vv = ngx_pcalloc(s->connection->pool,
sizeof(ngx_stream_variable_value_t));
if (vv == NULL) {
+ njs_vm_memory_error(vm);
return NJS_ERROR;
}
}
if (!(v->flags & NGX_STREAM_VAR_INDEXED)) {
- njs_vm_error(vm, "variable is not writable");
+ njs_vm_type_error(vm, "variable is not writable");
return NJS_ERROR;
}
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
status = (ngx_int_t) magic;
}
if (status < NGX_ABORT || status > NGX_STREAM_SERVICE_UNAVAILABLE) {
- return JS_ThrowInternalError(cx, "code is out of range");
+ return JS_ThrowRangeError(cx, "code is out of range");
}
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
if (ctx->filter) {
- return JS_ThrowInternalError(cx, "should not be called while "
- "filtering");
+ return JS_ThrowTypeError(cx, "should not be called while filtering");
}
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
for (n = 0; n < argc; n++) {
}
if (i == n) {
- (void) JS_ThrowInternalError(cx, "unknown event \"%.*s\"",
- (int) event.len, event.data);
+ (void) JS_ThrowTypeError(cx, "unknown event \"%.*s\"",
+ (int) event.len, event.data);
JS_FreeCString(cx, (char *) event.data);
return NULL;
}
for (n = 0; n < NGX_JS_EVENT_MAX; n++) {
type = ctx->events[n].data_type;
if (type != NGX_JS_UNSET && type != events[i].data_type) {
- (void) JS_ThrowInternalError(cx, "mixing string and buffer"
- " events is not allowed");
+ (void) JS_ThrowTypeError(cx, "mixing string and buffer"
+ " events is not allowed");
return NULL;
}
}
ses = JS_GetOpaque(this_val, NGX_QJS_CLASS_ID_STREAM_SESSION);
if (ses == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
ctx = ngx_stream_get_module_ctx(ses->session, ngx_stream_js_module);
}
if (JS_IsFunction(cx, ngx_qjs_arg(ctx->events[e->id].function))) {
- return JS_ThrowInternalError(cx, "event handler \"%s\" is already set",
- e->name.data);
+ return JS_ThrowTypeError(cx, "event handler \"%s\" is already set",
+ e->name.data);
}
if (!JS_IsFunction(cx, argv[1])) {
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
ses = JS_GetOpaque(this_val, NGX_QJS_CLASS_ID_STREAM_PERIODIC);
if (ses == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a periodic object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a periodic object");
}
obj = JS_NewObjectProtoClass(cx, JS_NULL, NGX_QJS_CLASS_ID_STREAM_VARS);
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
c = s->connection;
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
c = s->connection;
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
if (!ctx->filter) {
- return JS_ThrowInternalError(cx, "cannot send buffer in this handler");
+ return JS_ThrowTypeError(cx, "cannot send buffer in this handler");
}
/*
}
if (from_upstream == NGX_JS_BOOL_UNSET && ctx->buf == NULL) {
- return JS_ThrowInternalError(cx, "from_upstream flag is "
- "expected when called "
- "asynchronously");
+ return JS_ThrowTypeError(cx, "from_upstream flag is "
+ "expected when called "
+ "asynchronously");
}
}
}
JS_FreeValue(cx, buf);
- return JS_ThrowInternalError(cx, "memory error");
+ return JS_ThrowOutOfMemory(cx);
}
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
obj = JS_NewObjectProtoClass(cx, JS_NULL, NGX_QJS_CLASS_ID_STREAM_VARS);
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
prefix = NULL;
s = ngx_stream_qjs_session(this_val);
if (s == NULL) {
- return JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ return JS_ThrowTypeError(cx, "\"this\" is not a session object");
}
field = (ngx_uint_t *) ((u_char *) s + offset);
s = (ngx_stream_session_t *) ((uintptr_t) s & ~(uintptr_t) 1);
if (s == NULL) {
- (void) JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ (void) JS_ThrowTypeError(cx, "\"this\" is not a session object");
return -1;
}
s = (ngx_stream_session_t *) ((uintptr_t) s & ~(uintptr_t) 1);
if (s == NULL) {
- (void) JS_ThrowInternalError(cx, "\"this\" is not a session object");
+ (void) JS_ThrowTypeError(cx, "\"this\" is not a session object");
return -1;
}
JS_FreeCString(cx, (char *) name.data);
if (v == NULL) {
- (void) JS_ThrowInternalError(cx, "variable not found");
+ (void) JS_ThrowTypeError(cx, "variable not found");
return -1;
}
js_set $js_async test.asyncf;
js_set $js_async_direct test.asyncf_direct;
js_set $js_buffer test.buffer;
+ js_set $js_done_range test.done_range;
+ js_set $js_event_unknown test.event_unknown;
+ js_set $js_event_dup test.event_dup;
+ js_set $js_send_state test.send_state;
+ js_set $js_var_not_found test.var_not_found;
js_import test.js;
listen 127.0.0.1:8101;
return $js_buffer;
}
+
+ server {
+ listen 127.0.0.1:8103;
+ return $js_done_range;
+ }
+
+ server {
+ listen 127.0.0.1:8104;
+ return $js_event_unknown;
+ }
+
+ server {
+ listen 127.0.0.1:8105;
+ return $js_event_dup;
+ }
+
+ server {
+ listen 127.0.0.1:8106;
+ return $js_send_state;
+ }
+
+ server {
+ listen 127.0.0.1:8107;
+ return $js_var_not_found;
+ }
}
EOF
return `retval: \${a1 + a2}`;
}
+ function error_name(fn) {
+ try {
+ fn();
+ return 'no_error';
+
+ } catch (e) {
+ return `\${e.constructor.name}:\${e.message}`;
+ }
+ }
+
+ function done_range(s) {
+ return error_name(() => s.done(-999));
+ }
+
+ function event_unknown(s) {
+ return error_name(() => s.on('unknown', () => {}));
+ }
+
+ function event_dup(s) {
+ return error_name(() => {
+ s.on('upload', () => {});
+ s.on('upload', () => {});
+ });
+ }
+
+ function send_state(s) {
+ return error_name(() => s.send('x'));
+ }
+
+ function var_not_found(s) {
+ return error_name(() => { s.variables.unknown = 1 });
+ }
+
export default {njs:test_njs, addr, variable, sess_unk, log, access_step,
preread_step, filter_step, access_undecided, access_allow,
access_deny, preread_async, preread_data, preread_req_line,
req_line, filter_empty, filter_header_inject, filter_search,
access_except, preread_except, filter_except, asyncf,
- asyncf_direct, buffer};
+ asyncf_direct, buffer, done_range, event_unknown,
+ event_dup, send_state, var_not_found};
EOF
$t->run_daemon(\&stream_daemon, port(8090));
-$t->try_run('no stream njs available')->plan(25);
+$t->try_run('no stream njs available')->plan(30);
$t->waitforsocket('127.0.0.1:' . port(8090));
###############################################################################
}
+is(stream('127.0.0.1:' . port(8103))->read(),
+ 'RangeError:code is out of range', 's.done code range');
+like(stream('127.0.0.1:' . port(8104))->read(),
+ qr/^TypeError:unknown event "unknown"/, 's.on unknown event');
+like(stream('127.0.0.1:' . port(8105))->read(),
+ qr/^TypeError:event handler "upload" is already set/,
+ 's.on duplicate handler');
+is(stream('127.0.0.1:' . port(8106))->read(),
+ 'TypeError:cannot send buffer in this handler',
+ 's.send wrong handler');
+is(stream('127.0.0.1:' . port(8107))->read(),
+ 'TypeError:variable not found', 's.variables unknown set');
+
$t->stop();
ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'stream js log');