import { NextApiRequest , NextApiResponse } from 'next' ; import ErrorHandler from '../utils/errorHandler' ; function param { Error resorted export function onError ( err , req , res ) { err . statusCode = err . statusCode || 500 ; let error = { ... err } ; error . message = err . message ; if ( err . name === 'CastError' ) { const message = `Resource not found. Invalid: ${ err . path }` ; error = new ErrorHandler ( message , 400 ) ; } if ( err . name === 'ValidationError' ) { const message = Object . values ( err . errors ) . map ( ( err ) => err . message ) [ 0 ] ; error = new ErrorHandler ( message , 400 ) ; } res . status ( err . statusCode ) . json ( { success : false , error , message : error . message , stack : error . stack , } ) ; }