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
|
// Base class for custom application errors
|
||||||
// It extends Error and capture the stack trace
|
// It extends Error and capture the stack trace
|
||||||
class CustomError extends Error {
|
export class CustomError extends Error {
|
||||||
constructor () {
|
constructor () {
|
||||||
super();
|
super();
|
||||||
Error.captureStackTrace(this, this.constructor);
|
Error.captureStackTrace(this, this.constructor);
|
||||||
@@ -11,7 +9,7 @@ class CustomError extends Error {
|
|||||||
|
|
||||||
// NotAuthorized error with a 401 http error code
|
// NotAuthorized error with a 401 http error code
|
||||||
// used when a request is not authorized
|
// used when a request is not authorized
|
||||||
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;
|
||||||
@@ -23,7 +21,7 @@ class NotAuthorized extends CustomError {
|
|||||||
// BadRequest error with a 400 http error code
|
// BadRequest error with a 400 http error code
|
||||||
// used for requests not formatted correctly
|
// used for requests not formatted correctly
|
||||||
// TODO use for validation errors too?
|
// TODO use for validation errors too?
|
||||||
class BadRequest extends CustomError {
|
export class BadRequest extends CustomError {
|
||||||
constructor (customMessage) {
|
constructor (customMessage) {
|
||||||
super();
|
super();
|
||||||
this.name = this.constructor.name;
|
this.name = this.constructor.name;
|
||||||
@@ -34,7 +32,7 @@ class BadRequest extends CustomError {
|
|||||||
|
|
||||||
// InternalError error with a 500 http error code
|
// InternalError error with a 500 http error code
|
||||||
// used when an unexpected, internal server error is thrown
|
// used when an unexpected, internal server error is thrown
|
||||||
class InternalServerError extends CustomError {
|
export class InternalServerError extends CustomError {
|
||||||
constructor (customMessage) {
|
constructor (customMessage) {
|
||||||
super();
|
super();
|
||||||
this.name = this.constructor.name;
|
this.name = this.constructor.name;
|
||||||
@@ -42,10 +40,3 @@ class InternalServerError extends CustomError {
|
|||||||
this.message = customMessage || 'Internal server error.';
|
this.message = customMessage || 'Internal server error.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
CustomError,
|
|
||||||
NotAuthorized,
|
|
||||||
BadRequest,
|
|
||||||
InternalServerError
|
|
||||||
};
|
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
// Logger utility
|
// Logger utility
|
||||||
// TODO remove winston-mail and winston-newrelic if not used
|
// TODO remove winston-mail and winston-newrelic if not used
|
||||||
let winston = require('winston');
|
import winston from 'winston';
|
||||||
let nconf = require('nconf');
|
import nconf from 'nconf';
|
||||||
|
|
||||||
// TODO use const?
|
|
||||||
// TODO move isProd to a single location
|
// 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();
|
let logger = new winston.Logger();
|
||||||
|
|
||||||
@@ -19,4 +16,4 @@ if (isProd) {
|
|||||||
.add(winston.transports.Console);
|
.add(winston.transports.Console);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = logger;
|
export default logger;
|
||||||
|
|||||||
@@ -1,20 +1,18 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
// 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
|
||||||
let logger = require('../../libs/api-v3/logger');
|
import logger from '../../libs/api-v3/logger';
|
||||||
let errors = require('../../libs/api-v3/errors');
|
import {
|
||||||
let CustomError = errors.CustomError;
|
CustomError,
|
||||||
let InternalServerError = errors.InternalServerError;
|
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
|
// Log the original error with some metadata
|
||||||
let stack = err.stack || err.message || err;
|
let stack = err.stack || err.message || err;
|
||||||
|
|
||||||
logger.error(stack, {
|
logger.error(stack, {
|
||||||
originalUrl: req.originalUrl,
|
originalUrl: req.originalUrl,
|
||||||
headers: req.headers,
|
headers: req.headers,
|
||||||
body: req.body
|
body: req.body,
|
||||||
});
|
});
|
||||||
|
|
||||||
// In case of a CustomError class, use it's data
|
// In case of a CustomError class, use it's data
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
// This module is only used to attach middlewares to the express app
|
// 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
|
// Error handler middleware, define as the last one
|
||||||
app.use(errorHandler);
|
app.use(errorHandler);
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user