mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Use updated errors output format
This commit is contained in:
@@ -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 || {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
7
api/src/exceptions/invalid-otp.ts
Normal file
7
api/src/exceptions/invalid-otp.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { BaseException } from './base';
|
||||
|
||||
export class InvalidOTPException extends BaseException {
|
||||
constructor(message = 'Invalid user OTP.') {
|
||||
super(message, 401, 'INVALID_OTP');
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user