Skip to content

Commit

Permalink
fix(shell): router handler error
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jan 17, 2024
1 parent cbacba4 commit 3d627de
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 74 deletions.
49 changes: 30 additions & 19 deletions packages/shell/dist/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Object.defineProperty(exports, '__esModule', { value: true });

var mixme = require('mixme');
var plugAndPlay = require('plug-and-play');
var path = require('node:path');
var node_url = require('node:url');
var plugAndPlay = require('plug-and-play');
var stream = require('node:stream');
var pad = require('pad');

Expand Down Expand Up @@ -86,8 +86,6 @@ var index = /*#__PURE__*/Object.freeze({
filedirname: filedirname
});

filedirname((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));

var router = {
name: 'shell/plugins/router',
hooks: {
Expand Down Expand Up @@ -170,17 +168,17 @@ const route = function(context = {}, ...args) {
const route_call = (handler, command, params, err, args) => {
const config = this.config().get();
context = {
argv: process.argv.slice(2),
command: command,
error: err,
params: params,
args: args,
// argv: process.argv.slice(2),
stdin: config.router.stdin,
stdout: config.router.stdout,
stdout_end: config.router.stdout_end,
stderr: config.router.stderr,
stderr_end: config.router.stderr_end,
...context
...context,
command: command,
error: err,
params: params,
args: args,
};
return this.plugins.call_sync({
name: 'shell:router:call',
Expand Down Expand Up @@ -234,18 +232,33 @@ const route = function(context = {}, ...args) {
// handler = this.config.router.handler;
return route_error(err, command);
}
// Loader is
// - asynchronous and return a promise which fullfill with the handler function
// - synchronous and return the handler function
handler = route_load(handler);
if(handler.then){
return handler
.then(function(handler){
return route_call(handler, command, params, err, args);
})
.catch(async (err) => {
err.message = `Fail to load route. Message is: ${err.message}`;
return route_error(err, command);
})
.then(function (handler) {
return route_call(handler, command, params, err, args);
})
.catch(async (err) => {
return route_error(`Fail to load route. Message is: ${err.message}`, command);
});
}else {
return route_call(handler, command, params, err, args);
try {
const res = route_call(handler, command, params, err, args);
if (res?.catch) {
return res.catch(async (err) => {
await route_error(`Fail to load route. Message is: ${err.message}`, command);
throw err;
});
} else {
return res;
}
} catch (err) {
route_error(`Fail to load route. Message is: ${err.message}`, command);
throw err;
}
}
};
let params;
Expand Down Expand Up @@ -929,8 +942,6 @@ const compile = function(data, options = {}) {
return argv;
};

filedirname((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));

var help = {
name: 'shell/plugins/help',
hooks: {
Expand Down
4 changes: 2 additions & 2 deletions packages/shell/dist/cjs/routes/help.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Route Help
// Print the help information to stderr.

function help({argv, params, error, stderr, stderr_end}) {
function help({params, error, stderr, stderr_end}) {
const command = this.helping(params);
if (error) {
stderr.write(`\n${error.message}\n`);
stderr.write(`\n${typeof error === 'string' ? error : error.message}\n`);
}
stderr.write(this.help(command));
if (stderr_end) {
Expand Down
49 changes: 30 additions & 19 deletions packages/shell/dist/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { is_object_literal, mutate, clone, merge } from 'mixme';
import { plugandplay } from 'plug-and-play';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { plugandplay } from 'plug-and-play';
import stream from 'node:stream';
import pad from 'pad';

Expand Down Expand Up @@ -58,8 +58,6 @@ var index = /*#__PURE__*/Object.freeze({
filedirname: filedirname
});

filedirname(import.meta.url);

var router = {
name: 'shell/plugins/router',
hooks: {
Expand Down Expand Up @@ -142,17 +140,17 @@ const route = function(context = {}, ...args) {
const route_call = (handler, command, params, err, args) => {
const config = this.config().get();
context = {
argv: process.argv.slice(2),
command: command,
error: err,
params: params,
args: args,
// argv: process.argv.slice(2),
stdin: config.router.stdin,
stdout: config.router.stdout,
stdout_end: config.router.stdout_end,
stderr: config.router.stderr,
stderr_end: config.router.stderr_end,
...context
...context,
command: command,
error: err,
params: params,
args: args,
};
return this.plugins.call_sync({
name: 'shell:router:call',
Expand Down Expand Up @@ -206,18 +204,33 @@ const route = function(context = {}, ...args) {
// handler = this.config.router.handler;
return route_error(err, command);
}
// Loader is
// - asynchronous and return a promise which fullfill with the handler function
// - synchronous and return the handler function
handler = route_load(handler);
if(handler.then){
return handler
.then(function(handler){
return route_call(handler, command, params, err, args);
})
.catch(async (err) => {
err.message = `Fail to load route. Message is: ${err.message}`;
return route_error(err, command);
})
.then(function (handler) {
return route_call(handler, command, params, err, args);
})
.catch(async (err) => {
return route_error(`Fail to load route. Message is: ${err.message}`, command);
});
}else {
return route_call(handler, command, params, err, args);
try {
const res = route_call(handler, command, params, err, args);
if (res?.catch) {
return res.catch(async (err) => {
await route_error(`Fail to load route. Message is: ${err.message}`, command);
throw err;
});
} else {
return res;
}
} catch (err) {
route_error(`Fail to load route. Message is: ${err.message}`, command);
throw err;
}
}
};
let params;
Expand Down Expand Up @@ -901,8 +914,6 @@ const compile = function(data, options = {}) {
return argv;
};

filedirname(import.meta.url);

var help = {
name: 'shell/plugins/help',
hooks: {
Expand Down
4 changes: 2 additions & 2 deletions packages/shell/dist/esm/routes/help.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Route Help
// Print the help information to stderr.

function help({argv, params, error, stderr, stderr_end}) {
function help({params, error, stderr, stderr_end}) {
const command = this.helping(params);
if (error) {
stderr.write(`\n${error.message}\n`);
stderr.write(`\n${typeof error === 'string' ? error : error.message}\n`);
}
stderr.write(this.help(command));
if (stderr_end) {
Expand Down
40 changes: 26 additions & 14 deletions packages/shell/lib/plugins/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ const route = function(context = {}, ...args) {
const route_call = (handler, command, params, err, args) => {
const config = this.config().get();
context = {
argv: process.argv.slice(2),
command: command,
error: err,
params: params,
args: args,
// argv: process.argv.slice(2),
stdin: config.router.stdin,
stdout: config.router.stdout,
stdout_end: config.router.stdout_end,
stderr: config.router.stderr,
stderr_end: config.router.stderr_end,
...context
...context,
command: command,
error: err,
params: params,
args: args,
};
return this.plugins.call_sync({
name: 'shell:router:call',
Expand Down Expand Up @@ -158,15 +158,27 @@ const route = function(context = {}, ...args) {
handler = route_load(handler);
if(handler.then){
return handler
.then(function(handler){
return route_call(handler, command, params, err, args);
})
.catch(async (err) => {
err.message = `Fail to load route. Message is: ${err.message}`
return route_error(err, command);
})
.then(function (handler) {
return route_call(handler, command, params, err, args);
})
.catch(async (err) => {
return route_error(`Fail to load route. Message is: ${err.message}`, command);
});
}else{
return route_call(handler, command, params, err, args);
try {
const res = route_call(handler, command, params, err, args);
if (res?.catch) {
return res.catch(async (err) => {
await route_error(`Fail to load route. Message is: ${err.message}`, command);
throw err;
});
} else {
return res;
}
} catch (err) {
route_error(`Fail to load route. Message is: ${err.message}`, command);
throw err;
}
}
};
let params;
Expand Down
2 changes: 1 addition & 1 deletion packages/shell/lib/routes/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export default function({params, error, stderr, stderr_end}) {
const command = this.helping(params);
if (error) {
stderr.write(`\n${error.message}\n`);
stderr.write(`\n${typeof error === 'string' ? error : error.message}\n`);
}
stderr.write(this.help(command));
if (stderr_end) {
Expand Down
6 changes: 2 additions & 4 deletions packages/shell/test/config/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe 'config.main', ->
commands:
'server':
main: true
handler: () ->
console.log 'ok'
handler: (->)
strict: true
).should.throw [
'Invalid Main Configuration:'
Expand All @@ -39,8 +38,7 @@ describe 'config.main', ->
commands:
'server':
main: 'command'
handler: () ->
console.log 'ok'
handler: (->)
strict: true
).should.throw [
'Conflicting Main Value:'
Expand Down
2 changes: 1 addition & 1 deletion packages/shell/test/router/handler.argv.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { shell } from '../../lib/index.js'

describe 'handler.stdout', ->
describe 'handler.argv', ->

it "no argument", ->
app = shell
Expand Down
11 changes: 1 addition & 10 deletions packages/shell/test/router/handler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import fs from 'node:fs/promises'
import os from 'node:os'
import { shell } from '../../lib/index.js'

describe 'router.handler', ->

it 'context is parameter instance', ->
Expand All @@ -12,15 +12,6 @@ describe 'router.handler', ->
@should.have.property('parse').which.is.a.Function()
@should.have.property('compile').which.is.a.Function()
.route []

it 'propagate thrown error in sync handler', ->
(->
shell
options:
'my_argument': {}
handler: -> throw Error 'catch me'
.route ['--my_argument', 'my value']
).should.throw 'catch me'

it 'load with custom function handler', ->
await fs.writeFile "#{os.tmpdir()}/renamed_module.coffee", 'export default -> "Hello"'
Expand Down
Loading

0 comments on commit 3d627de

Please sign in to comment.