base: Fix error logging and reporting, make it better.
Some checks failed
/ deploy (push) Failing after -65h42m50s

This commit is contained in:
Jonatan Nilsson 2023-11-14 07:26:54 +00:00
parent a5c7e53802
commit 531c7acefe
4 changed files with 33 additions and 4 deletions

View file

@ -18,8 +18,8 @@ export default class AuthenticationRoutes {
/** GET: /api/authentication/login */ /** GET: /api/authentication/login */
async login(ctx) { async login(ctx) {
let res = await ctx.db.safeCallProc('auth_login', [ let res = await ctx.db.safeCallProc('auth_login', [
ctx.req.body.email, ctx.req.body.email || '',
ctx.req.body.password, ctx.req.body.password || '',
]) ])
let out = res.results[0][0] let out = res.results[0][0]

View file

@ -1,5 +1,6 @@
import MSSQL from 'msnodesqlv8' import MSSQL from 'msnodesqlv8'
import { HttpError } from 'flaska' import { HttpError } from 'flaska'
import { HttpErrorInternal } from './error.mjs'
export function initPool(core, config) { export function initPool(core, config) {
let pool = new MSSQL.Pool(config) let pool = new MSSQL.Pool(config)
@ -32,7 +33,7 @@ export function initPool(core, config) {
if (err.lineNumber && err.procName) { if (err.lineNumber && err.procName) {
message = `Error at ${err.procName}:${err.lineNumber} => ${message}` message = `Error at ${err.procName}:${err.lineNumber} => ${message}`
} }
throw new HttpError(500, message) throw new HttpErrorInternal(message, err)
}) })
}, },
promises: pool.promises, promises: pool.promises,

14
base/error.mjs Normal file
View file

@ -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
}
}

View file

@ -1,4 +1,4 @@
import { Flaska, QueryHandler, JsonHandler, FormidableHandler } from 'flaska' import { Flaska, QueryHandler, JsonHandler, FormidableHandler, HttpError } from 'flaska'
import formidable from 'formidable' import formidable from 'formidable'
import config from './config.mjs' import config from './config.mjs'
@ -48,6 +48,20 @@ export default class Server {
this.flaska.devMode() 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) { this.flaska.before(function(ctx) {
ctx.state.started = new Date().getTime() ctx.state.started = new Date().getTime()
ctx.req.ip = ctx.req.headers['x-forwarded-for'] || ctx.req.connection.remoteAddress ctx.req.ip = ctx.req.headers['x-forwarded-for'] || ctx.req.connection.remoteAddress