Skip to content

Instantly share code, notes, and snippets.

@gisderdube
Created November 13, 2018 14:55
Show Gist options
  • Save gisderdube/a634f39f1a5aba80211f6ee09897638b to your computer and use it in GitHub Desktop.
Save gisderdube/a634f39f1a5aba80211f6ee09897638b to your computer and use it in GitHub Desktop.
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