UNPKG

@di-zed/yandex-smart-home

Version:

The Yandex Smart Home skills for the different device types.

158 lines (157 loc) 4.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const i18n_1 = __importDefault(require("i18n")); const appError_1 = __importDefault(require("../errors/appError")); /** * Error Handler. */ class ErrorHandler { /** * Error Handler Constructor. * * @param err * @param req * @param res * @param next */ constructor(err, req, res, next) { /** * Status Code. * * @protected */ this.statusCode = 500; /** * Status. * * @protected */ this.status = 'error'; /** * Is Operational Error? * * @protected */ this.isOperational = false; if (process.env.NODE_ENV === 'development') { this.updateProperties(err); this.generateErrorDev(err, req, res); } else if (process.env.NODE_ENV === 'production') { let handledError = null; if (err.name === 'JsonWebTokenError') { handledError = this.handleJsonWebTokenError(); } else if (err.name === 'TokenExpiredError') { handledError = this.handleTokenExpiredError(); } this.updateProperties(handledError || err); this.generateErrorProd(handledError || err, req, res); } } /** * Generate an Error for the Development Mode. * * @param err * @param req * @param res * @returns Response | void * @protected */ generateErrorDev(err, req, res) { // REST API. if (req.originalUrl.startsWith('/v1.0')) { return res.status(this.statusCode).json({ status: this.status, message: err.message, error: err, stack: err.stack, }); } // Website. return res.status(this.statusCode).render('error', { title: res.__('Something went wrong!'), errorMessage: err.message, }); } /** * Generate an Error for the Production Mode. * * @param err * @param req * @param res * @returns Response | void * @protected */ generateErrorProd(err, req, res) { // REST API. if (req.originalUrl.startsWith('/v1.0')) { if (this.isOperational) { return res.status(this.statusCode).json({ status: this.status, message: err.message, }); } console.error('ERROR!', err); return res.status(500).json({ status: 'error', message: res.__('Something went wrong!'), }); } // Website. if (this.isOperational) { return res.status(this.statusCode).render('error', { title: res.__('Something went wrong!'), errorMessage: err.message, }); } console.error('ERROR!', err); return res.status(this.statusCode).render('error', { title: res.__('Something went wrong!'), errorMessage: res.__('Please try again later.'), }); } /** * Handle JSON Web Token Error. * * @returns AppError * @protected */ handleJsonWebTokenError() { return new appError_1.default(i18n_1.default.__('Invalid token. Please log in again!'), 401); } /** * Handle Token Expired Error. * * @returns AppError * @protected */ handleTokenExpiredError() { return new appError_1.default(i18n_1.default.__('Your token has expired! Please log in again.'), 401); } /** * Update Error Handler Properties. * * @param err * @returns ErrorHandler * @private */ updateProperties(err) { this.statusCode = 500; this.status = 'error'; this.isOperational = false; if (err instanceof appError_1.default) { this.statusCode = err.statusCode; this.status = err.status; this.isOperational = err.isOperational; } return this; } } function default_1(err, req, res, next) { return new ErrorHandler(err, req, res, next); } exports.default = default_1;