diff --git a/base/authentication/routes.mjs b/base/authentication/routes.mjs index 9176811..95f39cf 100644 --- a/base/authentication/routes.mjs +++ b/base/authentication/routes.mjs @@ -18,8 +18,8 @@ export default class AuthenticationRoutes { /** GET: /api/authentication/login */ async login(ctx) { let res = await ctx.db.safeCallProc('auth_login', [ - ctx.req.body.email, - ctx.req.body.password, + ctx.req.body.email || '', + ctx.req.body.password || '', ]) let out = res.results[0][0] diff --git a/base/db.mjs b/base/db.mjs index a6e16ae..ebe2a51 100644 --- a/base/db.mjs +++ b/base/db.mjs @@ -1,5 +1,6 @@ import MSSQL from 'msnodesqlv8' import { HttpError } from 'flaska' +import { HttpErrorInternal } from './error.mjs' export function initPool(core, config) { let pool = new MSSQL.Pool(config) @@ -32,7 +33,7 @@ export function initPool(core, config) { if (err.lineNumber && err.procName) { message = `Error at ${err.procName}:${err.lineNumber} => ${message}` } - throw new HttpError(500, message) + throw new HttpErrorInternal(message, err) }) }, promises: pool.promises, diff --git a/base/error.mjs b/base/error.mjs new file mode 100644 index 0000000..3a24413 --- /dev/null +++ b/base/error.mjs @@ -0,0 +1,14 @@ +import { HttpError } from 'flaska' + +export class HttpErrorInternal extends HttpError { + constructor(message, inner) { + super(500, message); + + Error.captureStackTrace(this, HttpError); + + let proto = Object.getPrototypeOf(this); + proto.name = 'HttpErrorInternal'; + + this.inner = inner + } +} \ No newline at end of file diff --git a/base/server.mjs b/base/server.mjs index f916053..bb813e0 100644 --- a/base/server.mjs +++ b/base/server.mjs @@ -1,4 +1,4 @@ -import { Flaska, QueryHandler, JsonHandler, FormidableHandler } from 'flaska' +import { Flaska, QueryHandler, JsonHandler, FormidableHandler, HttpError } from 'flaska' import formidable from 'formidable' import config from './config.mjs' @@ -48,6 +48,20 @@ export default class Server { this.flaska.devMode() } + this.flaska.onerror((err, ctx) => { + if (err instanceof HttpError && err.status !== 500) { + ctx.status = err.status + ctx.log.warn(err.message) + } else { + ctx.log.error(err.inner || err) + ctx.status = 500 + } + ctx.body = { + status: ctx.status, + message: err.message, + } + }) + this.flaska.before(function(ctx) { ctx.state.started = new Date().getTime() ctx.req.ip = ctx.req.headers['x-forwarded-for'] || ctx.req.connection.remoteAddress