Created
November 13, 2018 14:55
-
-
Save gisderdube/a634f39f1a5aba80211f6ee09897638b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CustomError = require('../CustomError') | |
const GET = req => { | |
// example for success | |
return { name: 'Rio de Janeiro' } | |
} | |
const POST = req => { | |
// example for unhandled error | |
throw new Error('Some unexpected error, may also be thrown by a library or the runtime.') | |
} | |
const DELETE = req => { | |
// example for handled error | |
throw new CustomError('CITY_NOT_FOUND', 404, 'The city you are trying to delete could not be found.') | |
} | |
const PATCH = req => { | |
// example for catching errors and using a CustomError | |
try { | |
// something bad happens here | |
throw new Error('Some internal error') | |
} catch (err) { | |
console.error(err) // decide what you want to do here | |
throw new CustomError( | |
'CITY_NOT_EDITABLE', | |
400, | |
'The city you are trying to edit is not editable.' | |
) | |
} | |
} | |
module.exports = { | |
GET, | |
POST, | |
DELETE, | |
PATCH, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment