From d5736e1178a201beb821220aefeca86bc7cd347f Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Wed, 4 Nov 2015 20:47:32 -0600 Subject: [PATCH] Convert classes to use Babel syntax --- website/src/libs/api-v3/errors.js | 17 ++++------------- website/src/libs/api-v3/logger.js | 11 ++++------- website/src/middlewares/api-v3/errorHandler.js | 18 ++++++++---------- website/src/middlewares/api-v3/index.js | 9 ++++----- 4 files changed, 20 insertions(+), 35 deletions(-) diff --git a/website/src/libs/api-v3/errors.js b/website/src/libs/api-v3/errors.js index c1ebec5d57..1439669d20 100644 --- a/website/src/libs/api-v3/errors.js +++ b/website/src/libs/api-v3/errors.js @@ -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 -}; \ No newline at end of file diff --git a/website/src/libs/api-v3/logger.js b/website/src/libs/api-v3/logger.js index 8a256a3461..b94d492b68 100644 --- a/website/src/libs/api-v3/logger.js +++ b/website/src/libs/api-v3/logger.js @@ -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; \ No newline at end of file +export default logger; diff --git a/website/src/middlewares/api-v3/errorHandler.js b/website/src/middlewares/api-v3/errorHandler.js index 833c03b508..d673e7af1c 100644 --- a/website/src/middlewares/api-v3/errorHandler.js +++ b/website/src/middlewares/api-v3/errorHandler.js @@ -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 }); -}; \ No newline at end of file +}; diff --git a/website/src/middlewares/api-v3/index.js b/website/src/middlewares/api-v3/index.js index 61ac54c4dc..54410c09a2 100644 --- a/website/src/middlewares/api-v3/index.js +++ b/website/src/middlewares/api-v3/index.js @@ -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); -}; \ No newline at end of file +};