When to return a Result.Error and when to throw an Exception?

Catch & Wrap ‘all the tings’?

Why return instead of throw

a technique that allows you to capture errors elegantly, without contaminating your code with ugly conditionals and try/ catch statements.

Other examples of implementations that favor return status over Exceptions

Relationship to “Checked” Exceptions

With great power comes great responsibility

Violation of the Open/Closed Principle?

Error handling samples

doSomething()
.pipe(match(
value => ctx.body = value,
err => ctx.statusCode = 500,
))
const result = doSomething()
.pipe(orDefault(“some-default-value”))
// if doSomething() fails, use “some-default-value” instead.
if (err instanceof RecordNotFound) {
ctx.body = { message }
ctx.status = 404
} else if (err instanceof CombinedValidationError) {
const { errors } = err
ctx.body = {
fields: combineErrors(errors),
message,
}
ctx.status = 400
} else if (err instanceof FieldValidationError) {
ctx.body = {
fields: { [err.fieldName]: err.error instanceof CombinedValidationError ? combineErrors(err.error.errors) : err.message },
message,
}
ctx.status = 400
} else if (err instanceof ValidationError) {
ctx.body = { message }
ctx.status = 400
} else if (err instanceof InvalidStateError) {
ctx.body = { message }
ctx.status = 422
} else if (err instanceof ForbiddenError) {
ctx.body = { message }
ctx.status = 403
} else if (err instanceof OptimisticLockError) {
ctx.status = 409
} else if (err instanceof CouldNotAquireDbLockError) {
ctx.status = 503
} else if (err instanceof ConnectionError) {
ctx.status = 504
} else {
// Unknown error
ctx.status = 500
}
try {
// execute the workflow
// handle success and error result cases
} catch (err) {
logger.error(err)
ctx.status = 500
}

Source

Other reads

What’s Next

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store