mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
fix tests, move most errors to shared code
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
// Base class for custom application errors
|
|
||||||
// It extends Error and capture the stack trace
|
|
||||||
export default class CustomError extends Error {
|
|
||||||
constructor () {
|
|
||||||
super();
|
|
||||||
Error.captureStackTrace(this, this.constructor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,37 @@
|
|||||||
import CustomError from './customError';
|
// Base class for custom application errors
|
||||||
|
// It extends Error and capture the stack trace
|
||||||
|
export class CustomError extends Error {
|
||||||
|
constructor () {
|
||||||
|
super();
|
||||||
|
Error.captureStackTrace(this, this.constructor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We specify an httpCode for all errors so that they can be used in the API too
|
||||||
|
|
||||||
export class NotAuthorized extends CustomError {
|
export class NotAuthorized extends CustomError {
|
||||||
constructor (customMessage) {
|
constructor (customMessage) {
|
||||||
super();
|
super();
|
||||||
this.name = this.constructor.name;
|
this.name = this.constructor.name;
|
||||||
|
this.httpCode = 401;
|
||||||
this.message = customMessage || 'Not authorized.';
|
this.message = customMessage || 'Not authorized.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class BadRequest extends CustomError {
|
||||||
|
constructor (customMessage) {
|
||||||
|
super();
|
||||||
|
this.name = this.constructor.name;
|
||||||
|
this.httpCode = 400;
|
||||||
|
this.message = customMessage || 'Bad request.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NotFound extends CustomError {
|
||||||
|
constructor (customMessage) {
|
||||||
|
super();
|
||||||
|
this.name = this.constructor.name;
|
||||||
|
this.httpCode = 404;
|
||||||
|
this.message = customMessage || 'Not found.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ describe('POST /user/class/cast/:spellId', () => {
|
|||||||
it('returns an error if user doesn\'t own the spell', async () => {
|
it('returns an error if user doesn\'t own the spell', async () => {
|
||||||
await expect(user.post(`/user/class/cast/snowball`))
|
await expect(user.post(`/user/class/cast/snowball`))
|
||||||
.to.eventually.be.rejected.and.eql({
|
.to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 401,
|
||||||
error: 'NotAuthorized',
|
error: 'NotAuthorized',
|
||||||
message: t('spellNotOwned'),
|
message: t('spellNotOwned'),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
// TODO move to shared tests
|
||||||
|
import { CustomError } from '../../../../../common/script/api-v3/errors';
|
||||||
import {
|
import {
|
||||||
CustomError,
|
|
||||||
NotAuthorized,
|
NotAuthorized,
|
||||||
BadRequest,
|
BadRequest,
|
||||||
InternalServerError,
|
InternalServerError,
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import responseMiddleware from '../../../../../website/src/middlewares/api-v3/re
|
|||||||
import getUserLanguage from '../../../../../website/src/middlewares/api-v3/getUserLanguage';
|
import getUserLanguage from '../../../../../website/src/middlewares/api-v3/getUserLanguage';
|
||||||
|
|
||||||
import { BadRequest } from '../../../../../website/src/libs/api-v3/errors';
|
import { BadRequest } from '../../../../../website/src/libs/api-v3/errors';
|
||||||
import { NotAuthorized as NotAuthorizedShared } from '../../../../../common/script/api-v3/errors';
|
|
||||||
import logger from '../../../../../website/src/libs/api-v3/logger';
|
import logger from '../../../../../website/src/libs/api-v3/logger';
|
||||||
|
|
||||||
describe('errorHandler', () => {
|
describe('errorHandler', () => {
|
||||||
@@ -87,21 +86,6 @@ describe('errorHandler', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handle CustomError(s) from shared code', () => {
|
|
||||||
let error = new NotAuthorizedShared();
|
|
||||||
|
|
||||||
errorHandler(error, req, res, next);
|
|
||||||
|
|
||||||
expect(res.status).to.be.calledOnce;
|
|
||||||
expect(res.json).to.be.calledOnce;
|
|
||||||
|
|
||||||
expect(res.status).to.be.calledWith(400);
|
|
||||||
expect(res.json).to.be.calledWith({
|
|
||||||
error: 'NotAuthorized',
|
|
||||||
message: 'Not authorized.',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('handle http-errors errors', () => {
|
it('handle http-errors errors', () => {
|
||||||
let error = new Error('custom message');
|
let error = new Error('custom message');
|
||||||
error.statusCode = 422;
|
error.statusCode = 422;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import CustomError from '../../../../common/script/api-v3/customError';
|
import { CustomError } from '../../../../common/script/api-v3/errors';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine NotAuthorized
|
* @apiDefine NotAuthorized
|
||||||
@@ -11,14 +11,7 @@ import CustomError from '../../../../common/script/api-v3/customError';
|
|||||||
* "message": "Not authorized."
|
* "message": "Not authorized."
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
export class NotAuthorized extends CustomError {
|
export { NotAuthorized } from '../../../../common/script/api-v3/errors';
|
||||||
constructor (customMessage) {
|
|
||||||
super();
|
|
||||||
this.name = this.constructor.name;
|
|
||||||
this.httpCode = 401;
|
|
||||||
this.message = customMessage || 'Not authorized.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine BadRequest
|
* @apiDefine BadRequest
|
||||||
@@ -31,14 +24,7 @@ export class NotAuthorized extends CustomError {
|
|||||||
* "message": "Bad request."
|
* "message": "Bad request."
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
export class BadRequest extends CustomError {
|
export { BadRequest } from '../../../../common/script/api-v3/errors';
|
||||||
constructor (customMessage) {
|
|
||||||
super();
|
|
||||||
this.name = this.constructor.name;
|
|
||||||
this.httpCode = 400;
|
|
||||||
this.message = customMessage || 'Bad request.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine NotFound
|
* @apiDefine NotFound
|
||||||
@@ -51,14 +37,7 @@ export class BadRequest extends CustomError {
|
|||||||
* "message": "Not found."
|
* "message": "Not found."
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
export class NotFound extends CustomError {
|
export { NotFound } from '../../../../common/script/api-v3/errors';
|
||||||
constructor (customMessage) {
|
|
||||||
super();
|
|
||||||
this.name = this.constructor.name;
|
|
||||||
this.httpCode = 404;
|
|
||||||
this.message = customMessage || 'Not found.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine InternalServerError
|
* @apiDefine InternalServerError
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// The error handler middleware that handles all errors
|
// The error handler middleware that handles all errors
|
||||||
// and respond to the client
|
// and respond to the client
|
||||||
import logger from '../../libs/api-v3/logger';
|
import logger from '../../libs/api-v3/logger';
|
||||||
import CustomError from '../../../../common/script/api-v3/customError';
|
import { CustomError } from '../../../../common/script/api-v3/errors';
|
||||||
import {
|
import {
|
||||||
BadRequest,
|
BadRequest,
|
||||||
InternalServerError,
|
InternalServerError,
|
||||||
@@ -24,12 +24,6 @@ export default function errorHandler (err, req, res, next) { // eslint-disable-l
|
|||||||
// If we can't identify it, respond with a generic 500 error
|
// If we can't identify it, respond with a generic 500 error
|
||||||
let responseErr = err instanceof CustomError ? err : null;
|
let responseErr = err instanceof CustomError ? err : null;
|
||||||
|
|
||||||
// TODO don't return always 400 for errors in common code
|
|
||||||
// If CustomError but without httpCode then they come from shared code, treat as 400s
|
|
||||||
if (err instanceof CustomError && !err.httpCode) {
|
|
||||||
err.httpCode = 400;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle errors created with 'http-errors' or similar that have a status/statusCode property
|
// Handle errors created with 'http-errors' or similar that have a status/statusCode property
|
||||||
if (err.statusCode && typeof err.statusCode === 'number') {
|
if (err.statusCode && typeof err.statusCode === 'number') {
|
||||||
responseErr = new CustomError();
|
responseErr = new CustomError();
|
||||||
|
|||||||
Reference in New Issue
Block a user