Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom service error type support #329

Merged

Conversation

mdurling
Copy link

Minor change to allow custom error types so that system isn't locked into the predefined set.
This is a totally backwards compatible change. The following code segments are equivalent:

if (isServiceError(error)) {
  if (error.type === 'NotFoundError') {
    return res.status(404).send({ error })
  }
}
if (isServiceError(error, 'NotFoundError')) {
    return res.status(404).send({ error })
  }
}

Services can now return a custom error:

return ServiceError({ type: 'CustomError', message: 'Custom error message' })

Which can be handled using the new signature:

if (isServiceError(error, 'CustomError')) {
    return res.status(400).send({ error })
  }
}

Copy link

@levi217 levi217 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It might make sense to define a base service error type to express response middleware to do some out of the box handling for well known situations like 404, 500, 401, 400, etc. Otherwise we'll see various apis implement subsets of this. Even custom scenarios could extend on this. Just a thought.

export interface ServiceErrorDescriptor {
name: '__ServiceErrorDescriptor__'
type: 'ServiceException' | 'NotFoundError' | 'ConflictError' | 'ValidationError'
export const ServiceErrorDescriptorTypes = [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love for this to inherit from mds-utils/exceptions.ts if possible at some point

@mdurling mdurling merged commit 6c23c7a into develop May 18, 2020
@mdurling mdurling deleted the feature/michael/support-custom-service-error-types branch May 18, 2020 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants