Use updated errors output format

This commit is contained in:
rijkvanzanten
2020-08-25 15:26:35 -04:00
parent 31d63e0e4d
commit d87038ee5d
5 changed files with 44 additions and 26 deletions

View File

@@ -1,10 +1,13 @@
export class BaseException extends Error {
status: number;
code: string;
extensions: Record<string, any>;
constructor(message: string, status: number, code: string) {
constructor(message: string, status: number, code: string, extensions?: Record<string, any>) {
super(message);
this.status = status;
this.code = code;
this.extensions = extensions || {};
}
}

View File

@@ -3,6 +3,7 @@ export * from './collection-not-found';
export * from './field-not-found';
export * from './forbidden';
export * from './invalid-credentials';
export * from './invalid-otp';
export * from './invalid-payload';
export * from './invalid-query';
export * from './item-limit';

View File

@@ -0,0 +1,7 @@
import { BaseException } from './base';
export class InvalidOTPException extends BaseException {
constructor(message = 'Invalid user OTP.') {
super(message, 401, 'INVALID_OTP');
}
}

View File

@@ -4,41 +4,48 @@ import logger from '../logger';
import env from '../env';
const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
let payload: any;
if (err instanceof BaseException) {
logger.debug(err);
res.status(err.status);
const payload: any = {
error: {
code: err.code,
message: err.message,
},
payload = {
errors: [
{
message: err.message,
extensions: {
...err.extensions,
code: err.code,
}
}
],
};
if (env.NODE_ENV === 'development') {
payload.error.stack = err.stack;
}
return res.json(payload);
} else {
logger.error(err);
res.status(500);
const payload: any = {
error: {
code: 'INTERNAL_SERVER_ERROR',
message: err.message,
},
payload = {
errors: [
{
message: err.message,
extensions: {
code: 'INTERNAL_SERVER_ERROR',
}
}
],
};
if (env.NODE_ENV === 'development') {
payload.error.stack = err.stack;
}
return res.json(payload);
}
if (env.NODE_ENV === 'development') {
payload.errors[0].extensions.exception = {
stack: err.stack
}
}
return res.json(payload);
};
export default errorHandler;

View File

@@ -3,7 +3,7 @@ import jwt from 'jsonwebtoken';
import argon2 from 'argon2';
import { nanoid } from 'nanoid';
import ms from 'ms';
import { InvalidCredentialsException, InvalidPayloadException } from '../exceptions';
import { InvalidCredentialsException, InvalidPayloadException, InvalidOTPException } from '../exceptions';
import { Session, Accountability, AbstractServiceOptions, Action } from '../types';
import Knex from 'knex';
import ActivityService from '../services/activity';
@@ -51,14 +51,14 @@ export default class AuthenticationService {
}
if (user.tfa_secret && !otp) {
throw new InvalidPayloadException(`"otp" is required`);
throw new InvalidOTPException(`"otp" is required`);
}
if (user.tfa_secret && otp) {
const otpValid = await this.verifyOTP(user.id, otp);
if (otpValid === false) {
throw new InvalidPayloadException(`"otp" is invalid`);
throw new InvalidOTPException(`"otp" is invalid`);
}
}