import { Request , Response , NextFunction } from 'express' ; import * as Err from '../exceptions/exceptions' ; import { ErrorWithFields } from '../exceptions/exceptions' ; import * as fs from 'fs' ; import * as i18next from 'i18next' ; import { responseErrorWithObject } from '../helpers/responses' ; export default function handle ( err , req , res , next ) { let status ; const lang = req . acceptsLanguages ( ) || 'en' ; const langPath = __dirname + `/../resources/locales/${ lang }/errors.json` ; const translations = fs . existsSync ( langPath ) ? require ( langPath ) : null ; i18next . init ( { lng : lang . toString ( ) , resources : translations } ) ; switch ( err . constructor ) { case Err . KycFailedError : case Err . KycAlreadyVerifiedError : case Err . KycPendingError : case Err . InsufficientEthBalance : case Err . AuthenticatorError : status = 400 ; break ; case Err . InvalidPassword : case Err . UserNotActivated : case Err . UserActivated : status = 403 ; break ; case Err . VerificationIsNotFound : case Err . UserNotFound : status = 404 ; break ; case Err . UserExists : case Err . NotCorrectVerificationCode : case Err . ReferralDoesNotExist : case Err . InviteIsNotAllowed : case Err . MaxVerificationsAttemptsReached : case Err . IncorrectMnemonic : case Err . ReferralIsNotActivated : status = 422 ; break ; default : status = 500 ; console . error ( err . message ) ; console . error ( err . stack ) ; } responseErrorWithObject ( res , { 'message' : i18next . t ( err . message , err . fields ) } , status ) ; }