summaryrefslogtreecommitdiffstats
path: root/error.go
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-08-18 13:46:10 +0200
committerclsr <clsr@clsr.net>2017-08-18 13:46:10 +0200
commitda77deba78c8a7447b4a38324d2422a5df293b26 (patch)
treebb94b8e18eaf27a86e9fc21c4342d9e0a605baf7 /error.go
downloadcnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.tar.gz
cnp-go-da77deba78c8a7447b4a38324d2422a5df293b26.zip
Initial commitv0.1.0
Diffstat (limited to 'error.go')
-rw-r--r--error.go205
1 files changed, 205 insertions, 0 deletions
diff --git a/error.go b/error.go
new file mode 100644
index 0000000..b6270b9
--- /dev/null
+++ b/error.go
@@ -0,0 +1,205 @@
+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()
+}