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 interfaces and callbacks for basic error types #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ChrysmOre
Copy link
Contributor

@ChrysmOre ChrysmOre commented Feb 9, 2022

What

Added array of basic interfaces and callback helper functions for asserting error types.

As embedding HTTP status codes to errors was not considered reasonable an alternative is to provide interfaces
that represent different error types. Using this we can reflect on an error and whether it conforms to a particular behaviour and act accordingly.
The advantage of interfaces vs concrete types is lack of tight coupling, and allowing basic error behaviour to be shared amongst different package types. An errNotFound can be used to represent a URL not found or a database document etc.
These interfaces could be implemented separately in each service, they don't cause a dependency in that sense, but to have them here as helpers will be more efficient and can also serve as a guide for which interfaces to use, keeping consistency and not reinventing the wheel etc

To make use have your errors implement these interfaces and you can then callback to check the error implements the error type. Example error:

type Error{
 notFound bool
}

// this func implements the errNotFound interface
func (e *Error) NotFound() bool
  return e.notFound
 }
...
// In package i.e. datastore
if err != nil{
  return Error{notFound: true}
 }
 .... 
 // in handler
 if dperrors.NotFound(err){
   status = http.StatusNotFound
 }

How to review

Check changes make sense

Who can review

Anyone

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.

2 participants