package cnp // Error represents an error as used in a CNP error response. type Error interface { // CNPError returns the value of the "error" parameter in a CNP error // response. CNPError() string Error() string } const ( // ReasonSyntax represents the "syntax" reason parameter value. ReasonSyntax = "syntax" // ReasonVersion represents the "version" reason parameter value. ReasonVersion = "version" // ReasonInvalid represents the "invalid" reason parameter value. ReasonInvalid = "invalid" // ReasonNotSupported represents the "not_supported" reason parameter value. ReasonNotSupported = "not_supported" // ReasonTooLarge represents the "too_large" reason parameter value. ReasonTooLarge = "too_large" // ReasonNotFound represents the "not_found" reason parameter value. ReasonNotFound = "not_found" // ReasonDenied represents the "denied" reason parameter value. ReasonDenied = "denied" // ReasonRejected represents the "rejected" reason parameter value. ReasonRejected = "rejected" // ReasonServerError represents the "server_error" reason parameter value. ReasonServerError = "server_error" ) // NewError returns a new Error based on a reason parameter value. // // If the reason is blank, nil is returned. // If the reason is unknown, ErrorServerError is returned. func NewError(reason string) Error { switch reason { case ReasonSyntax: return ErrorSyntax{"Invalid CNP Message Syntax"} case ReasonVersion: return ErrorVersion{"Unsupported CNP Protocol Version"} case ReasonInvalid: return ErrorInvalid{"Invalid CNP Message"} case ReasonNotSupported: return ErrorNotSupported{"CNP Feature Not Supported"} case ReasonTooLarge: return ErrorTooLarge{"CNP Message Too Large"} case ReasonNotFound: return ErrorNotFound{"Not Found"} case ReasonDenied: return ErrorDenied{"Denied"} case ReasonRejected: return ErrorRejected{"Rejected"} case "": return nil default: fallthrough case ReasonServerError: return ErrorServerError{"Internal Server Error"} } } // ErrorSyntax represents the CNP "syntax" error reason. type ErrorSyntax struct { Reason string } // CNPError on ErrorSyntax returns the error parameter value "syntax". func (e ErrorSyntax) CNPError() string { return ReasonSyntax } func (e ErrorSyntax) Error() string { return "CNP syntax error: " + e.Reason } // ErrorVersion represents the CNP "version" error reason. type ErrorVersion struct { Reason string } // CNPError on ErrorVersion returns the error parameter value "version". func (e ErrorVersion) CNPError() string { return ReasonVersion } func (e ErrorVersion) Error() string { return "Unsupported CNP version: " + e.Reason } // ErrorInvalid represents the CNP "invalid" error reason. type ErrorInvalid struct { Reason string } // CNPError on ErrorInvalid returns the error parameter value "invalid". func (e ErrorInvalid) CNPError() string { return ReasonInvalid } func (e ErrorInvalid) Error() string { return "Invalid CNP message: " + e.Reason } // ErrorNotSupported represents the CNP "not_supported" error reason. type ErrorNotSupported struct { Reason string } // CNPError on ErrorNotSupported returns the error parameter value // "not_supported". func (e ErrorNotSupported) CNPError() string { return ReasonNotSupported } func (e ErrorNotSupported) Error() string { return "Requested CNP feature is not supported: " + e.Reason } // ErrorTooLarge represents the CNP "too_large" error reason. type ErrorTooLarge struct { Reason string } // CNPError on ErrorTooLarge returns the error parameter value "too_large". func (e ErrorTooLarge) CNPError() string { return ReasonTooLarge } func (e ErrorTooLarge) Error() string { return "CNP message is too large: " + e.Reason } // ErrorNotFound represents the CNP "not_found" error reason. type ErrorNotFound struct { Reason string } // CNPError on ErrorNotFound returns the error parameter value "not_found". func (e ErrorNotFound) CNPError() string { return ReasonNotFound } func (e ErrorNotFound) Error() string { return "Requested path was not found: " + e.Reason } // ErrorDenied represents the CNP "denied" error reason. type ErrorDenied struct { Reason string } // CNPError on ErrorDenied returns the error parameter value "denied". func (e ErrorDenied) CNPError() string { return ReasonDenied } func (e ErrorDenied) Error() string { return "Server denied access: " + e.Reason } // ErrorRejected represents the CNP "rejected" error reason. type ErrorRejected struct { Reason string } // CNPError on ErrorRejected returns the error parameter value "rejected". func (e ErrorRejected) CNPError() string { return ReasonRejected } func (e ErrorRejected) Error() string { return "Server rejected request: " + e.Reason } // ErrorServerError represents the CNP "server_error" error reason. type ErrorServerError struct { Reason string } // CNPError on ErrorServerError returns the error parameter value // "server_error". func (e ErrorServerError) CNPError() string { return ReasonServerError } func (e ErrorServerError) Error() string { return "Internal server error: " + e.Reason } // ErrorURL is a non-CNPError that represents an invalid CNP URL. type ErrorURL struct { // Err represents the error reason. Err error // URL is the URL that triggered the error. URL string } func (e ErrorURL) Error() string { if e.Err == nil { return "CNP URL error" } return e.Err.Error() }