package ratchet import ( "errors" "fmt" ) const INTERNAL_ERROR_MESSAGE = "internal error" // Application error codes. // // Note: these are generic codes but map well to HTTP status codes. const ( EINTERNAL = "internal" ENOTFOUND = "not_found" ) // Error respresents an application specific-error. Application errors can be // unwrapped by the caller to extract out the code and message // // Any non-application error (like disk error) should be reported as an EINTERNAL // error and the human user should only see "Internal error" as the message. // These low-level internal error details should only be logged and reported to // the operation of the application, not the end user. type Error struct { // Machine-readable error code. Code string // Human-readable error message. Message string } // Error implements the error interface. Not used by the application otherwise. func (e Error) Error() string { return fmt.Sprintf("ratchet error: code=%s message=%s", e.Code, e.Message) } // ErrorCode unwraps an application error and returns its code. // Non-application errors always return EINTERNAL. func ErrorCode(err error) string { var e *Error if err == nil { return "" } else if errors.As(err, &e) { return e.Code } return EINTERNAL } // ErrorMessage unwraps an application error and returns it's message. // Non-application errors always return "Internal error". func ErrorMessage(err error) string { var e *Error if err == nil { return "" } else if errors.As(err, &e) { return e.Message } return INTERNAL_ERROR_MESSAGE }