mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
Convert classes to use Babel syntax
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
// Base class for custom application errors
|
||||
// It extends Error and capture the stack trace
|
||||
class CustomError extends Error {
|
||||
export class CustomError extends Error {
|
||||
constructor () {
|
||||
super();
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
@@ -11,7 +9,7 @@ class CustomError extends Error {
|
||||
|
||||
// NotAuthorized error with a 401 http error code
|
||||
// used when a request is not authorized
|
||||
class NotAuthorized extends CustomError {
|
||||
export class NotAuthorized extends CustomError {
|
||||
constructor (customMessage) {
|
||||
super();
|
||||
this.name = this.constructor.name;
|
||||
@@ -23,7 +21,7 @@ class NotAuthorized extends CustomError {
|
||||
// BadRequest error with a 400 http error code
|
||||
// used for requests not formatted correctly
|
||||
// TODO use for validation errors too?
|
||||
class BadRequest extends CustomError {
|
||||
export class BadRequest extends CustomError {
|
||||
constructor (customMessage) {
|
||||
super();
|
||||
this.name = this.constructor.name;
|
||||
@@ -34,7 +32,7 @@ class BadRequest extends CustomError {
|
||||
|
||||
// InternalError error with a 500 http error code
|
||||
// used when an unexpected, internal server error is thrown
|
||||
class InternalServerError extends CustomError {
|
||||
export class InternalServerError extends CustomError {
|
||||
constructor (customMessage) {
|
||||
super();
|
||||
this.name = this.constructor.name;
|
||||
@@ -42,10 +40,3 @@ class InternalServerError extends CustomError {
|
||||
this.message = customMessage || 'Internal server error.';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CustomError,
|
||||
NotAuthorized,
|
||||
BadRequest,
|
||||
InternalServerError
|
||||
};
|
||||
@@ -1,13 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
// Logger utility
|
||||
// TODO remove winston-mail and winston-newrelic if not used
|
||||
let winston = require('winston');
|
||||
let nconf = require('nconf');
|
||||
import winston from 'winston';
|
||||
import nconf from 'nconf';
|
||||
|
||||
// TODO use const?
|
||||
// TODO move isProd to a single location
|
||||
let isProd = nconf.get('NODE_ENV') === 'production';
|
||||
const isProd = nconf.get('NODE_ENV') === 'production';
|
||||
|
||||
let logger = new winston.Logger();
|
||||
|
||||
@@ -19,4 +16,4 @@ if (isProd) {
|
||||
.add(winston.transports.Console);
|
||||
}
|
||||
|
||||
module.exports = logger;
|
||||
export default logger;
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
// The error handler middleware that handles all errors
|
||||
// and respond to the client
|
||||
let logger = require('../../libs/api-v3/logger');
|
||||
let errors = require('../../libs/api-v3/errors');
|
||||
let CustomError = errors.CustomError;
|
||||
let InternalServerError = errors.InternalServerError;
|
||||
import logger from '../../libs/api-v3/logger';
|
||||
import {
|
||||
CustomError,
|
||||
InternalServerError,
|
||||
} from '../../libs/api-v3/errors';
|
||||
|
||||
module.exports = function errorHandlerMiddleware (err, req, res, next) {
|
||||
export default function errorHandler (err, req, res, next) {
|
||||
// Log the original error with some metadata
|
||||
let stack = err.stack || err.message || err;
|
||||
|
||||
logger.error(stack, {
|
||||
originalUrl: req.originalUrl,
|
||||
headers: req.headers,
|
||||
body: req.body
|
||||
body: req.body,
|
||||
});
|
||||
|
||||
// In case of a CustomError class, use it's data
|
||||
@@ -37,4 +35,4 @@ module.exports = function errorHandlerMiddleware (err, req, res, next) {
|
||||
error: responseErr.name,
|
||||
message: responseErr.message
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
// This module is only used to attach middlewares to the express app
|
||||
|
||||
let errorHandler = require('./errorHandler');
|
||||
import errorHandler from './errorHandler';
|
||||
|
||||
export default function middleware (app) {
|
||||
|
||||
module.exports = function attachMiddlewares (app) {
|
||||
// Error handler middleware, define as the last one
|
||||
app.use(errorHandler);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user