Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1286 from gomezd/context

Handle invalid context exceptions with output handler
  • Loading branch information...
commit c7de16f8671c381e1c415deea608f114eedb7602 2 parents 2d5b916 + 216cd4a
lzhan authored
View
7 lib/app/autoload/dispatch.server.js
@@ -122,7 +122,12 @@ YUI.add('mojito-dispatcher', function (Y, NAME) {
// HookSystem::StartBlock
Y.mojito.hooks.hook('dispatch', adapter.hook, 'start', command);
// HookSystem::EndBlock
- store.validateContext(command.context);
+ try {
+ store.validateContext(command.context);
+ } catch (err) {
+ adapter.error(err);
+ return;
+ }
if (command.rpc) {
// forcing to dispatch command through RPC tunnel
View
40 tests/unit/lib/app/autoload/test-dispatch.server.js
@@ -7,7 +7,7 @@ YUI.add('mojito-dispatcher-server-tests', function(Y, NAME) {
var suite = new Y.Test.Suite(NAME),
A = Y.Assert,
- dispatcher = Y.mojito.Dispatcher,
+ dispatcher,
store,
command,
adapter;
@@ -17,6 +17,7 @@ YUI.add('mojito-dispatcher-server-tests', function(Y, NAME) {
name: 'dispatch',
'setUp': function() {
+ dispatcher = Y.mojito.util.heir(Y.mojito.Dispatcher);
store = {
getStaticAppConfig: function() {
return { yui: {} };
@@ -57,6 +58,7 @@ YUI.add('mojito-dispatcher-server-tests', function(Y, NAME) {
},
'tearDown': function() {
+ dispatcher = null;
store = null;
command = null;
adapter = null;
@@ -119,8 +121,7 @@ YUI.add('mojito-dispatcher-server-tests', function(Y, NAME) {
'test dispatch with valid controller': function () {
var tunnel,
- acCommand,
- _createActionContext = dispatcher._createActionContext;
+ acCommand;
errorTriggered = false;
dispatcher.init(store, tunnel);
@@ -142,15 +143,11 @@ YUI.add('mojito-dispatcher-server-tests', function(Y, NAME) {
}
});
A.areSame(command, acCommand, 'AC should be created based on the original command');
-
- // restoring references
- dispatcher._createActionContext = _createActionContext;
},
'test dispatch with invalid controller': function () {
var tunnel,
- adapterErrorCalled,
- _createActionContext = dispatcher._createActionContext;
+ adapterErrorCalled;
errorTriggered = false;
dispatcher.init(store, tunnel);
@@ -168,9 +165,32 @@ YUI.add('mojito-dispatcher-server-tests', function(Y, NAME) {
}
});
A.isTrue(adapterErrorCalled, 'adapter.error should be called for invalid controllers');
+ },
+
+ 'test dispatch with invalid context': function () {
+ var tunnel,
+ adapterError,
+ adapterErrorCalled;
- // restoring references
- dispatcher._createActionContext = _createActionContext;
+ dispatcher.init(store, tunnel);
+ // if the expandInstance calls with an error, the tunnel
+ // should be tried.
+ store.validateContext = function (context) {
+ var error = new Error('Invalid context dimension key "foo"');
+ error.code = 400; //bad request
+ throw error;
+ };
+ dispatcher._createActionContext = function (c) {
+ A.fail('adapter.error should be called instead');
+ };
+ dispatcher.dispatch(command, {
+ error: function (error) {
+ adapterError = error;
+ adapterErrorCalled = true;
+ }
+ });
+ A.isTrue(adapterErrorCalled, 'adapter.error should be called for invalid context');
+ A.areEqual(400, adapterError.code, 'adapter.error should be called with custom error');
},
'test dispatch with rpc and tunnel': function () {
Please sign in to comment.
Something went wrong with that request. Please try again.