From ab4070bfdfcdab9ed11140a703a07691c0ca7337 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Mon, 14 Aug 2023 06:56:09 -0700 Subject: [PATCH] made .ErrHTTP() method return the HTTP status code (as an int). added some missing error. removed some errors that should not have been there. --- README.md | 35 ++++++++++++-- badgateway.go | 8 +++- badrequest.go | 8 +++- conflict.go | 8 +++- error.go | 2 +- expectationfailed.go | 8 +++- faileddependency.go | 8 +++- forbidden.go | 8 +++- gatewaytimeout.go | 8 +++- gone.go | 8 +++- httpversionnotsupported.go | 8 +++- insufficientstorage.go | 8 +++- internalservererror.go | 8 +++- lengthrequired.go | 8 +++- locked.go | 8 +++- loopdetected.go | 8 +++- methodnotallowed.go | 8 +++- misdirectedrequest.go | 46 +++++++++++++++++++ networkauthenticationrequired.go | 8 +++- notacceptable.go | 8 +++- notextended.go | 8 +++- notfound.go | 8 +++- notimplemented.go | 8 +++- payloadtoolarge.go | 42 ----------------- paymentrequired.go | 8 +++- preconditionfailed.go | 8 +++- preconditionrequired.go | 8 +++- proxyauthrequired.go | 8 +++- requestedrangenotsatisfiable.go | 8 +++- requestentitytoolarge.go | 8 +++- requestheaderfieldstoolarge.go | 8 +++- requesttimeout.go | 8 +++- requesturitoolong.go | 8 +++- serviceunavailable.go | 8 +++- teapot.go | 8 +++- tooearly.go | 46 +++++++++++++++++++ toomanyrequests.go | 8 +++- unauthorized.go | 8 +++- unavailableforlegalreasons.go | 8 +++- unprocessableentity.go | 8 +++- unsupportedmediatype.go | 8 +++- upgraderequired.go | 8 +++- uritoolong.go | 42 ----------------- ...onegotiates.go => variantalsonegotiates.go | 8 +++- 44 files changed, 352 insertions(+), 165 deletions(-) create mode 100644 misdirectedrequest.go delete mode 100644 payloadtoolarge.go create mode 100644 tooearly.go delete mode 100644 uritoolong.go rename variantaisonegotiates.go => variantalsonegotiates.go (88%) diff --git a/README.md b/README.md index ecac81d..00cd1bc 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,12 @@ Here is an example of wrapping an error: //@TODO case errhttp.InternalServerError: //@TODO - + case errhttp.ClientError: //@TODO case errhttp.ServerError: //@TODO - + default: //@TODO } @@ -59,12 +59,39 @@ Here is an example of using one of the package global variable errors: //@TODO case errhttp.InternalServerError: //@TODO - + case errhttp.ClientError: //@TODO case errhttp.ServerError: //@TODO - + + default: + //@TODO + } +``` + +Here is another example, where it used the `.ErrHTTP()` method to get the HTTP response status code: + +```go + import "sourcecode.social/reiver/go-errhttp" + + // ... + + return errhttp.ErrBadRequest + + // ... + + switch casted := err.(type) { + case errhttp.ClientError: + statuscode := casted.ErrHTTP() + + http.Error(responsewriter, http.StatusText(statuscode), statuscode) + return + case errhttp.ServerError: + statuscode := casted.ErrHTTP() + + http.Error(responsewriter, http.StatusText(statuscode), statuscode) + return default: //@TODO } diff --git a/badgateway.go b/badgateway.go index 75b7384..4c6c010 100644 --- a/badgateway.go +++ b/badgateway.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalBadGateway{} var _ ServerError = internalBadGateway{} var _ BadGateway = internalBadGateway{} @@ -27,8 +31,8 @@ func (receiver internalBadGateway) Error() string { return receiver.err.Error() } -func (internalBadGateway) ErrHTTP() { - // Nothing here. +func (internalBadGateway) ErrHTTP() int { + return http.StatusBadGateway } func (internalBadGateway) ServerError() { diff --git a/badrequest.go b/badrequest.go index 7a93667..4bb2e71 100644 --- a/badrequest.go +++ b/badrequest.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalBadRequest{} var _ ClientError = internalBadRequest{} var _ BadRequest = internalBadRequest{} @@ -25,8 +29,8 @@ func (receiver internalBadRequest) Error() string { return receiver.err.Error() } -func (internalBadRequest) ErrHTTP() { - // Nothing here. +func (internalBadRequest) ErrHTTP() int { + return http.StatusBadRequest } func (internalBadRequest) ClientError() { diff --git a/conflict.go b/conflict.go index 1844523..8da9697 100644 --- a/conflict.go +++ b/conflict.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalConflict{} var _ ClientError = internalConflict{} var _ Conflict = internalConflict{} @@ -25,8 +29,8 @@ func (receiver internalConflict) Error() string { return receiver.err.Error() } -func (internalConflict) ErrHTTP() { - // Nothing here. +func (internalConflict) ErrHTTP() int { + return http.StatusConflict } func (internalConflict) ClientError() { diff --git a/error.go b/error.go index 783eb0f..22d4a64 100644 --- a/error.go +++ b/error.go @@ -2,6 +2,6 @@ package errhttp type Error interface { error - ErrHTTP() + ErrHTTP() int Unwrap() error } diff --git a/expectationfailed.go b/expectationfailed.go index 2f1efb8..f91c205 100644 --- a/expectationfailed.go +++ b/expectationfailed.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalExpectationFailed{} var _ ClientError = internalExpectationFailed{} var _ ExpectationFailed = internalExpectationFailed{} @@ -25,8 +29,8 @@ func (receiver internalExpectationFailed) Error() string { return receiver.err.Error() } -func (internalExpectationFailed) ErrHTTP() { - // Nothing here. +func (internalExpectationFailed) ErrHTTP() int { + return http.StatusExpectationFailed } func (internalExpectationFailed) ClientError() { diff --git a/faileddependency.go b/faileddependency.go index a3cf487..95d2556 100644 --- a/faileddependency.go +++ b/faileddependency.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalFailedDependency{} var _ ClientError = internalFailedDependency{} var _ FailedDependency = internalFailedDependency{} @@ -25,8 +29,8 @@ func (receiver internalFailedDependency) Error() string { return receiver.err.Error() } -func (internalFailedDependency) ErrHTTP() { - // Nothing here. +func (internalFailedDependency) ErrHTTP() int { + return http.StatusFailedDependency } func (internalFailedDependency) ClientError() { diff --git a/forbidden.go b/forbidden.go index 0b5262c..f8f2fc5 100644 --- a/forbidden.go +++ b/forbidden.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalForbidden{} var _ ClientError = internalForbidden{} var _ Forbidden = internalForbidden{} @@ -25,8 +29,8 @@ func (receiver internalForbidden) Error() string { return receiver.err.Error() } -func (internalForbidden) ErrHTTP() { - // Nothing here. +func (internalForbidden) ErrHTTP() int { + return http.StatusForbidden } func (internalForbidden) ClientError() { diff --git a/gatewaytimeout.go b/gatewaytimeout.go index b1081f1..1e29e1a 100644 --- a/gatewaytimeout.go +++ b/gatewaytimeout.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalGatewayTimeout{} var _ ServerError = internalGatewayTimeout{} var _ GatewayTimeout = internalGatewayTimeout{} @@ -27,8 +31,8 @@ func (receiver internalGatewayTimeout) Error() string { return receiver.err.Error() } -func (internalGatewayTimeout) ErrHTTP() { - // Nothing here. +func (internalGatewayTimeout) ErrHTTP() int { + return http.StatusGatewayTimeout } func (internalGatewayTimeout) ServerError() { diff --git a/gone.go b/gone.go index ff5f551..20ddf46 100644 --- a/gone.go +++ b/gone.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalGone{} var _ ClientError = internalGone{} var _ Gone = internalGone{} @@ -25,8 +29,8 @@ func (receiver internalGone) Error() string { return receiver.err.Error() } -func (internalGone) ErrHTTP() { - // Nothing here. +func (internalGone) ErrHTTP() int { + return http.StatusGone } func (internalGone) ClientError() { diff --git a/httpversionnotsupported.go b/httpversionnotsupported.go index c948418..a53e528 100644 --- a/httpversionnotsupported.go +++ b/httpversionnotsupported.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalHTTPVersionNotSupported{} var _ ServerError = internalHTTPVersionNotSupported{} var _ HTTPVersionNotSupported = internalHTTPVersionNotSupported{} @@ -27,8 +31,8 @@ func (receiver internalHTTPVersionNotSupported) Error() string { return receiver.err.Error() } -func (internalHTTPVersionNotSupported) ErrHTTP() { - // Nothing here. +func (internalHTTPVersionNotSupported) ErrHTTP() int { + return http.StatusHTTPVersionNotSupported } func (internalHTTPVersionNotSupported) ServerError() { diff --git a/insufficientstorage.go b/insufficientstorage.go index 2fc83f3..c91d406 100644 --- a/insufficientstorage.go +++ b/insufficientstorage.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalInsufficientStorage{} var _ ServerError = internalInsufficientStorage{} var _ InsufficientStorage = internalInsufficientStorage{} @@ -27,8 +31,8 @@ func (receiver internalInsufficientStorage) Error() string { return receiver.err.Error() } -func (internalInsufficientStorage) ErrHTTP() { - // Nothing here. +func (internalInsufficientStorage) ErrHTTP() int { + return http.StatusInsufficientStorage } func (internalInsufficientStorage) ServerError() { diff --git a/internalservererror.go b/internalservererror.go index ee5c611..50e3297 100644 --- a/internalservererror.go +++ b/internalservererror.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalInternalServerError{} var _ ServerError = internalInternalServerError{} var _ InternalServerError = internalInternalServerError{} @@ -27,8 +31,8 @@ func (receiver internalInternalServerError) Error() string { return receiver.err.Error() } -func (internalInternalServerError) ErrHTTP() { - // Nothing here. +func (internalInternalServerError) ErrHTTP() int { + return http.StatusInternalServerError } func (internalInternalServerError) ServerError() { diff --git a/lengthrequired.go b/lengthrequired.go index 7b22833..e46c7e0 100644 --- a/lengthrequired.go +++ b/lengthrequired.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalLengthRequired{} var _ ClientError = internalLengthRequired{} var _ LengthRequired = internalLengthRequired{} @@ -25,8 +29,8 @@ func (receiver internalLengthRequired) Error() string { return receiver.err.Error() } -func (internalLengthRequired) ErrHTTP() { - // Nothing here. +func (internalLengthRequired) ErrHTTP() int { + return http.StatusLengthRequired } func (internalLengthRequired) ClientError() { diff --git a/locked.go b/locked.go index 8e08705..fe85732 100644 --- a/locked.go +++ b/locked.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalLocked{} var _ ClientError = internalLocked{} var _ Locked = internalLocked{} @@ -25,8 +29,8 @@ func (receiver internalLocked) Error() string { return receiver.err.Error() } -func (internalLocked) ErrHTTP() { - // Nothing here. +func (internalLocked) ErrHTTP() int { + return http.StatusLocked } func (internalLocked) ClientError() { diff --git a/loopdetected.go b/loopdetected.go index 8b26ed0..fb1739e 100644 --- a/loopdetected.go +++ b/loopdetected.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalLoopDetected{} var _ ServerError = internalLoopDetected{} var _ LoopDetected = internalLoopDetected{} @@ -27,8 +31,8 @@ func (receiver internalLoopDetected) Error() string { return receiver.err.Error() } -func (internalLoopDetected) ErrHTTP() { - // Nothing here. +func (internalLoopDetected) ErrHTTP() int { + return http.StatusLoopDetected } func (internalLoopDetected) ServerError() { diff --git a/methodnotallowed.go b/methodnotallowed.go index 2813ff8..e47b79f 100644 --- a/methodnotallowed.go +++ b/methodnotallowed.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalMethodNotAllowed{} var _ ClientError = internalMethodNotAllowed{} var _ MethodNotAllowed = internalMethodNotAllowed{} @@ -25,8 +29,8 @@ func (receiver internalMethodNotAllowed) Error() string { return receiver.err.Error() } -func (internalMethodNotAllowed) ErrHTTP() { - // Nothing here. +func (internalMethodNotAllowed) ErrHTTP() int { + return http.StatusMethodNotAllowed } func (internalMethodNotAllowed) ClientError() { diff --git a/misdirectedrequest.go b/misdirectedrequest.go new file mode 100644 index 0000000..ca6a645 --- /dev/null +++ b/misdirectedrequest.go @@ -0,0 +1,46 @@ +package errhttp + +import ( + "net/http" +) + +var _ Error = internalMisdirectedRequest{} +var _ ClientError = internalMisdirectedRequest{} +var _ MisdirectedRequest = internalMisdirectedRequest{} + +var ErrMisdirectedRequest = MisdirectedRequestWrap(nil) + +type MisdirectedRequest interface { + ClientError + MisdirectedRequest() +} + +type internalMisdirectedRequest struct { + err error +} + +func MisdirectedRequestWrap(err error) error { + return internalMisdirectedRequest{ + err:err, + } +} + +func (receiver internalMisdirectedRequest) Error() string { + return receiver.err.Error() +} + +func (internalMisdirectedRequest) ErrHTTP() int { + return http.StatusMisdirectedRequest +} + +func (internalMisdirectedRequest) ClientError() { + // Nothing here. +} + +func (internalMisdirectedRequest) MisdirectedRequest() { + // Nothing here. +} + +func (receiver internalMisdirectedRequest) Unwrap() error { + return receiver.err +} diff --git a/networkauthenticationrequired.go b/networkauthenticationrequired.go index 2b71901..cdd2f50 100644 --- a/networkauthenticationrequired.go +++ b/networkauthenticationrequired.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalNetworkAuthenticationRequired{} var _ ServerError = internalNetworkAuthenticationRequired{} var _ NetworkAuthenticationRequired = internalNetworkAuthenticationRequired{} @@ -27,8 +31,8 @@ func (receiver internalNetworkAuthenticationRequired) Error() string { return receiver.err.Error() } -func (internalNetworkAuthenticationRequired) ErrHTTP() { - // Nothing here. +func (internalNetworkAuthenticationRequired) ErrHTTP() int { + return http.StatusNetworkAuthenticationRequired } func (internalNetworkAuthenticationRequired) ServerError() { diff --git a/notacceptable.go b/notacceptable.go index 3a08de1..8417348 100644 --- a/notacceptable.go +++ b/notacceptable.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalNotAcceptable{} var _ ClientError = internalNotAcceptable{} var _ NotAcceptable = internalNotAcceptable{} @@ -25,8 +29,8 @@ func (receiver internalNotAcceptable) Error() string { return receiver.err.Error() } -func (internalNotAcceptable) ErrHTTP() { - // Nothing here. +func (internalNotAcceptable) ErrHTTP() int { + return http.StatusNotAcceptable } func (internalNotAcceptable) ClientError() { diff --git a/notextended.go b/notextended.go index 9f11847..71b3468 100644 --- a/notextended.go +++ b/notextended.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalNotExtended{} var _ ServerError = internalNotExtended{} var _ NotExtended = internalNotExtended{} @@ -27,8 +31,8 @@ func (receiver internalNotExtended) Error() string { return receiver.err.Error() } -func (internalNotExtended) ErrHTTP() { - // Nothing here. +func (internalNotExtended) ErrHTTP() int { + return http.StatusNotExtended } func (internalNotExtended) ServerError() { diff --git a/notfound.go b/notfound.go index bbd78ae..7cb8a34 100644 --- a/notfound.go +++ b/notfound.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalNotFound{} var _ ClientError = internalNotFound{} var _ NotFound = internalNotFound{} @@ -25,8 +29,8 @@ func (receiver internalNotFound ) Error() string { return receiver.err.Error() } -func (internalNotFound ) ErrHTTP() { - // Nothing here. +func (internalNotFound ) ErrHTTP() int { + return http.StatusNotFound } func (internalNotFound ) ClientError() { diff --git a/notimplemented.go b/notimplemented.go index 4a49a8a..3bd7e92 100644 --- a/notimplemented.go +++ b/notimplemented.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalNotImplemented{} var _ ServerError = internalNotImplemented{} var _ NotImplemented = internalNotImplemented{} @@ -27,8 +31,8 @@ func (receiver internalNotImplemented) Error() string { return receiver.err.Error() } -func (internalNotImplemented) ErrHTTP() { - // Nothing here. +func (internalNotImplemented) ErrHTTP() int { + return http.StatusNotImplemented } func (internalNotImplemented) ServerError() { diff --git a/payloadtoolarge.go b/payloadtoolarge.go deleted file mode 100644 index 6a711e7..0000000 --- a/payloadtoolarge.go +++ /dev/null @@ -1,42 +0,0 @@ -package errhttp - -var _ Error = internalPayloadTooLarge{} -var _ ClientError = internalPayloadTooLarge{} -var _ PayloadTooLarge = internalPayloadTooLarge{} - -var ErrPayloadTooLarge error = PayloadTooLargeWrap(nil) - -type PayloadTooLarge interface { - ClientError - PayloadTooLarge() -} - -type internalPayloadTooLarge struct { - err error -} - -func PayloadTooLargeWrap(err error) error { - return internalPayloadTooLarge{ - err:err, - } -} - -func (receiver internalPayloadTooLarge) Error() string { - return receiver.err.Error() -} - -func (internalPayloadTooLarge) ErrHTTP() { - // Nothing here. -} - -func (internalPayloadTooLarge) ClientError() { - // Nothing here. -} - -func (internalPayloadTooLarge) PayloadTooLarge() { - // Nothing here. -} - -func (receiver internalPayloadTooLarge) Unwrap() error { - return receiver.err -} diff --git a/paymentrequired.go b/paymentrequired.go index 3631a49..57f8343 100644 --- a/paymentrequired.go +++ b/paymentrequired.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalPaymentRequired{} var _ ClientError = internalPaymentRequired{} var _ PaymentRequired = internalPaymentRequired{} @@ -25,8 +29,8 @@ func (receiver internalPaymentRequired) Error() string { return receiver.err.Error() } -func (internalPaymentRequired) ErrHTTP() { - // Nothing here. +func (internalPaymentRequired) ErrHTTP() int { + return http.StatusPaymentRequired } diff --git a/preconditionfailed.go b/preconditionfailed.go index 38955bf..c88324f 100644 --- a/preconditionfailed.go +++ b/preconditionfailed.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalPreconditionFailed{} var _ ClientError = internalPreconditionFailed{} var _ PreconditionFailed = internalPreconditionFailed{} @@ -25,8 +29,8 @@ func (receiver internalPreconditionFailed) Error() string { return receiver.err.Error() } -func (internalPreconditionFailed) ErrHTTP() { - // Nothing here. +func (internalPreconditionFailed) ErrHTTP() int { + return http.StatusPreconditionFailed } func (internalPreconditionFailed) ClientError() { diff --git a/preconditionrequired.go b/preconditionrequired.go index bbfa59f..f8a4826 100644 --- a/preconditionrequired.go +++ b/preconditionrequired.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalPreconditionRequired{} var _ ClientError = internalPreconditionRequired{} var _ PreconditionRequired = internalPreconditionRequired{} @@ -25,8 +29,8 @@ func (receiver internalPreconditionRequired) Error() string { return receiver.err.Error() } -func (internalPreconditionRequired) ErrHTTP() { - // Nothing here. +func (internalPreconditionRequired) ErrHTTP() int { + return http.StatusPreconditionRequired } func (internalPreconditionRequired) ClientError() { diff --git a/proxyauthrequired.go b/proxyauthrequired.go index cd23e64..7488f23 100644 --- a/proxyauthrequired.go +++ b/proxyauthrequired.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalProxyAuthRequired{} var _ ClientError = internalProxyAuthRequired{} var _ ProxyAuthRequired = internalProxyAuthRequired{} @@ -25,8 +29,8 @@ func (receiver internalProxyAuthRequired) Error() string { return receiver.err.Error() } -func (internalProxyAuthRequired) ErrHTTP() { - // Nothing here. +func (internalProxyAuthRequired) ErrHTTP() int { + return http.StatusProxyAuthRequired } func (internalProxyAuthRequired) ClientError() { diff --git a/requestedrangenotsatisfiable.go b/requestedrangenotsatisfiable.go index 5e1436d..a230d93 100644 --- a/requestedrangenotsatisfiable.go +++ b/requestedrangenotsatisfiable.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalRequestedRangeNotSatisfiable{} var _ ClientError = internalRequestedRangeNotSatisfiable{} var _ RequestedRangeNotSatisfiable = internalRequestedRangeNotSatisfiable{} @@ -25,8 +29,8 @@ func (receiver internalRequestedRangeNotSatisfiable) Error() string { return receiver.err.Error() } -func (internalRequestedRangeNotSatisfiable) ErrHTTP() { - // Nothing here. +func (internalRequestedRangeNotSatisfiable) ErrHTTP() int { + return http.StatusRequestedRangeNotSatisfiable } func (internalRequestedRangeNotSatisfiable) ClientError() { diff --git a/requestentitytoolarge.go b/requestentitytoolarge.go index 3506adc..868ee9e 100644 --- a/requestentitytoolarge.go +++ b/requestentitytoolarge.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalRequestEntityTooLarge{} var _ ClientError = internalRequestEntityTooLarge{} var _ RequestEntityTooLarge = internalRequestEntityTooLarge{} @@ -25,8 +29,8 @@ func (receiver internalRequestEntityTooLarge) Error() string { return receiver.err.Error() } -func (internalRequestEntityTooLarge) ErrHTTP() { - // Nothing here. +func (internalRequestEntityTooLarge) ErrHTTP() int { + return http.StatusRequestEntityTooLarge } func (internalRequestEntityTooLarge) ClientError() { diff --git a/requestheaderfieldstoolarge.go b/requestheaderfieldstoolarge.go index 469fa02..cd06ec2 100644 --- a/requestheaderfieldstoolarge.go +++ b/requestheaderfieldstoolarge.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalRequestHeaderFieldsTooLarge{} var _ ClientError = internalRequestHeaderFieldsTooLarge{} var _ RequestHeaderFieldsTooLarge = internalRequestHeaderFieldsTooLarge{} @@ -25,8 +29,8 @@ func (receiver internalRequestHeaderFieldsTooLarge) Error() string { return receiver.err.Error() } -func (internalRequestHeaderFieldsTooLarge) ErrHTTP() { - // Nothing here. +func (internalRequestHeaderFieldsTooLarge) ErrHTTP() int { + return http.StatusRequestHeaderFieldsTooLarge } func (internalRequestHeaderFieldsTooLarge) ClientError() { diff --git a/requesttimeout.go b/requesttimeout.go index 9a8cf98..443ebd1 100644 --- a/requesttimeout.go +++ b/requesttimeout.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalRequestTimeout{} var _ ClientError = internalRequestTimeout{} var _ RequestTimeout = internalRequestTimeout{} @@ -25,8 +29,8 @@ func (receiver internalRequestTimeout) Error() string { return receiver.err.Error() } -func (internalRequestTimeout) ErrHTTP() { - // Nothing here. +func (internalRequestTimeout) ErrHTTP() int { + return http.StatusRequestTimeout } func (internalRequestTimeout) ClientError() { diff --git a/requesturitoolong.go b/requesturitoolong.go index 28143f1..ef7c86f 100644 --- a/requesturitoolong.go +++ b/requesturitoolong.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalRequestURITooLong{} var _ ClientError = internalRequestURITooLong{} var _ RequestURITooLong = internalRequestURITooLong{} @@ -25,8 +29,8 @@ func (receiver internalRequestURITooLong) Error() string { return receiver.err.Error() } -func (internalRequestURITooLong) ErrHTTP() { - // Nothing here. +func (internalRequestURITooLong) ErrHTTP() int { + return http.StatusRequestURITooLong } func (internalRequestURITooLong) ClientError() { diff --git a/serviceunavailable.go b/serviceunavailable.go index da67b1e..171632a 100644 --- a/serviceunavailable.go +++ b/serviceunavailable.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalServiceUnavailable{} var _ ServerError = internalServiceUnavailable{} var _ ServiceUnavailable = internalServiceUnavailable{} @@ -25,8 +29,8 @@ func (receiver internalServiceUnavailable) Error() string { return receiver.err.Error() } -func (internalServiceUnavailable) ErrHTTP() { - // Nothing here. +func (internalServiceUnavailable) ErrHTTP() int { + return http.StatusServiceUnavailable } func (internalServiceUnavailable) ServerError() { diff --git a/teapot.go b/teapot.go index 986edf7..0dbf9cc 100644 --- a/teapot.go +++ b/teapot.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalTeapot{} var _ ClientError = internalTeapot{} var _ Teapot = internalTeapot{} @@ -25,8 +29,8 @@ func (receiver internalTeapot) Error() string { return receiver.err.Error() } -func (internalTeapot) ErrHTTP() { - // Nothing here. +func (internalTeapot) ErrHTTP() int { + return http.StatusTeapot } func (internalTeapot) ClientError() { diff --git a/tooearly.go b/tooearly.go new file mode 100644 index 0000000..9ed67da --- /dev/null +++ b/tooearly.go @@ -0,0 +1,46 @@ +package errhttp + +import ( + "net/http" +) + +var _ Error = internalTooEarly{} +var _ ClientError = internalTooEarly{} +var _ TooEarly = internalTooEarly{} + +var ErrTooEarly = TooEarlyWrap(nil) + +type TooEarly interface { + ClientError + TooEarly() +} + +type internalTooEarly struct { + err error +} + +func TooEarlyWrap(err error) error { + return internalTooEarly{ + err:err, + } +} + +func (receiver internalTooEarly) Error() string { + return receiver.err.Error() +} + +func (internalTooEarly) ErrHTTP() int { + return http.StatusTooEarly +} + +func (internalTooEarly) ClientError() { + // Nothing here. +} + +func (internalTooEarly) TooEarly() { + // Nothing here. +} + +func (receiver internalTooEarly) Unwrap() error { + return receiver.err +} diff --git a/toomanyrequests.go b/toomanyrequests.go index 843f9be..f990983 100644 --- a/toomanyrequests.go +++ b/toomanyrequests.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalTooManyRequests{} var _ ClientError = internalTooManyRequests{} var _ TooManyRequests = internalTooManyRequests{} @@ -25,8 +29,8 @@ func (receiver internalTooManyRequests) Error() string { return receiver.err.Error() } -func (internalTooManyRequests) ErrHTTP() { - // Nothing here. +func (internalTooManyRequests) ErrHTTP() int { + return http.StatusTooManyRequests } func (internalTooManyRequests) ClientError() { diff --git a/unauthorized.go b/unauthorized.go index 952c7ab..8fbe842 100644 --- a/unauthorized.go +++ b/unauthorized.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalUnauthorized{} var _ ClientError = internalUnauthorized{} var _ Unauthorized = internalUnauthorized{} @@ -25,8 +29,8 @@ func (receiver internalUnauthorized) Error() string { return receiver.err.Error() } -func (internalUnauthorized) ErrHTTP() { - // Nothing here. +func (internalUnauthorized) ErrHTTP() int { + return http.StatusUnauthorized } func (internalUnauthorized) ClientError() { diff --git a/unavailableforlegalreasons.go b/unavailableforlegalreasons.go index 7f7a0ba..6fa544b 100644 --- a/unavailableforlegalreasons.go +++ b/unavailableforlegalreasons.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalUnavailableForLegalReasons{} var _ ClientError = internalUnavailableForLegalReasons{} var _ UnavailableForLegalReasons = internalUnavailableForLegalReasons{} @@ -25,8 +29,8 @@ func (receiver internalUnavailableForLegalReasons) Error() string { return receiver.err.Error() } -func (internalUnavailableForLegalReasons) ErrHTTP() { - // Nothing here. +func (internalUnavailableForLegalReasons) ErrHTTP() int { + return http.StatusUnavailableForLegalReasons } func (internalUnavailableForLegalReasons) ClientError() { diff --git a/unprocessableentity.go b/unprocessableentity.go index a19cc9e..a9e07ff 100644 --- a/unprocessableentity.go +++ b/unprocessableentity.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalUnprocessableEntity{} var _ ClientError = internalUnprocessableEntity{} var _ UnprocessableEntity = internalUnprocessableEntity{} @@ -25,8 +29,8 @@ func (receiver internalUnprocessableEntity) Error() string { return receiver.err.Error() } -func (internalUnprocessableEntity) ErrHTTP() { - // Nothing here. +func (internalUnprocessableEntity) ErrHTTP() int { + return http.StatusUnprocessableEntity } func (internalUnprocessableEntity) ClientError() { diff --git a/unsupportedmediatype.go b/unsupportedmediatype.go index c945810..31e9dab 100644 --- a/unsupportedmediatype.go +++ b/unsupportedmediatype.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalUnsupportedMediaType{} var _ ClientError = internalUnsupportedMediaType{} var _ UnsupportedMediaType = internalUnsupportedMediaType{} @@ -25,8 +29,8 @@ func (receiver internalUnsupportedMediaType) Error() string { return receiver.err.Error() } -func (internalUnsupportedMediaType) ErrHTTP() { - // Nothing here. +func (internalUnsupportedMediaType) ErrHTTP() int { + return http.StatusUnsupportedMediaType } func (internalUnsupportedMediaType) ClientError() { diff --git a/upgraderequired.go b/upgraderequired.go index c95f887..c97f02b 100644 --- a/upgraderequired.go +++ b/upgraderequired.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalUpgradeRequired{} var _ ClientError = internalUpgradeRequired{} var _ UpgradeRequired = internalUpgradeRequired{} @@ -25,8 +29,8 @@ func (receiver internalUpgradeRequired) Error() string { return receiver.err.Error() } -func (internalUpgradeRequired) ErrHTTP() { - // Nothing here. +func (internalUpgradeRequired) ErrHTTP() int { + return http.StatusUpgradeRequired } func (internalUpgradeRequired) ClientError() { diff --git a/uritoolong.go b/uritoolong.go deleted file mode 100644 index c4066e8..0000000 --- a/uritoolong.go +++ /dev/null @@ -1,42 +0,0 @@ -package errhttp - -var _ Error = internalURITooLong{} -var _ ClientError = internalURITooLong{} -var _ URITooLong = internalURITooLong{} - -var ErrURITooLong error = URITooLongWrap(nil) - -type URITooLong interface { - ClientError - URITooLong() -} - -type internalURITooLong struct { - err error -} - -func URITooLongWrap(err error) error { - return internalURITooLong{ - err:err, - } -} - -func (receiver internalURITooLong) Error() string { - return receiver.err.Error() -} - -func (internalURITooLong) ErrHTTP() { - // Nothing here. -} - -func (internalURITooLong) ClientError() { - // Nothing here. -} - -func (internalURITooLong) URITooLong() { - // Nothing here. -} - -func (receiver internalURITooLong) Unwrap() error { - return receiver.err -} diff --git a/variantaisonegotiates.go b/variantalsonegotiates.go similarity index 88% rename from variantaisonegotiates.go rename to variantalsonegotiates.go index f2940fc..75f44a3 100644 --- a/variantaisonegotiates.go +++ b/variantalsonegotiates.go @@ -1,5 +1,9 @@ package errhttp +import ( + "net/http" +) + var _ Error = internalVariantAlsoNegotiates{} var _ ServerError = internalVariantAlsoNegotiates{} var _ VariantAlsoNegotiates = internalVariantAlsoNegotiates{} @@ -27,8 +31,8 @@ func (receiver internalVariantAlsoNegotiates) Error() string { return receiver.err.Error() } -func (internalVariantAlsoNegotiates) ErrHTTP() { - // Nothing here. +func (internalVariantAlsoNegotiates) ErrHTTP() int { + return http.StatusVariantAlsoNegotiates } func (internalVariantAlsoNegotiates) ServerError() {