Package errhttp provides errors and types that make dealing with HTTP response errors easier, for the Go programming language.
Go to file
Charles Iliya Krempeaux f09738021e some fixes 2023-10-01 11:30:28 +09:00
LICENSE initial commit 2017-02-06 12:57:57 -08:00
README.md made .ErrHTTP() method return the HTTP status code (as an int). added some missing error. removed some errors that should not have been there. 2023-08-14 06:56:09 -07:00
badgateway.go some fixes 2023-10-01 11:30:28 +09:00
badrequest.go some fixes 2023-10-01 11:30:28 +09:00
clienterror.go client errors 2017-02-06 12:58:13 -08:00
conflict.go some fixes 2023-10-01 11:30:28 +09:00
errbadgateway_test.go some fixes 2023-10-01 11:30:28 +09:00
errbadrequest_test.go some fixes 2023-10-01 11:30:28 +09:00
errconflict_test.go some fixes 2023-10-01 11:30:28 +09:00
errexpectationfailed_test.go some fixes 2023-10-01 11:30:28 +09:00
errfaileddependency_test.go some fixes 2023-10-01 11:30:28 +09:00
errforbidden_test.go some fixes 2023-10-01 11:30:28 +09:00
errgatewaytimeout_test.go some fixes 2023-10-01 11:30:28 +09:00
errgone_test.go some fixes 2023-10-01 11:30:28 +09:00
errhttpversionnotsupported_test.go some fixes 2023-10-01 11:30:28 +09:00
errinsufficientstorage_test.go some fixes 2023-10-01 11:30:28 +09:00
errinternalservererror_test.go some fixes 2023-10-01 11:30:28 +09:00
errlengthrequired_test.go some fixes 2023-10-01 11:30:28 +09:00
errlocked_test.go some fixes 2023-10-01 11:30:28 +09:00
errloopdetected_test.go some fixes 2023-10-01 11:30:28 +09:00
errmethodnotallowed_test.go some fixes 2023-10-01 11:30:28 +09:00
errmisdirectedrequest_test.go some fixes 2023-10-01 11:30:28 +09:00
errnetworkauthenticationrequired_test.go some fixes 2023-10-01 11:30:28 +09:00
errnotacceptable_test.go some fixes 2023-10-01 11:30:28 +09:00
errnotextended_test.go some fixes 2023-10-01 11:30:28 +09:00
errnotfound_test.go some fixes 2023-10-01 11:30:28 +09:00
errnotimplemented_test.go some fixes 2023-10-01 11:30:28 +09:00
error.go made .ErrHTTP() method return the HTTP status code (as an int). added some missing error. removed some errors that should not have been there. 2023-08-14 06:56:09 -07:00
errpaymentrequired_test.go some fixes 2023-10-01 11:30:28 +09:00
errpreconditionfailed_test.go some fixes 2023-10-01 11:30:28 +09:00
errpreconditionrequired_test.go some fixes 2023-10-01 11:30:28 +09:00
errproxyauthrequired_test.go some fixes 2023-10-01 11:30:28 +09:00
errrequestedrangenotsatisfiable_test.go some fixes 2023-10-01 11:30:28 +09:00
errrequestentitytoolarge_test.go some fixes 2023-10-01 11:30:28 +09:00
errrequestheaderfieldstoolarge_test.go some fixes 2023-10-01 11:30:28 +09:00
errrequesttimeout_test.go some fixes 2023-10-01 11:30:28 +09:00
errrequesturitoolong_test.go some fixes 2023-10-01 11:30:28 +09:00
errserviceunavailable_test.go some fixes 2023-10-01 11:30:28 +09:00
errteapot_test.go some fixes 2023-10-01 11:30:28 +09:00
errtooearly_test.go some fixes 2023-10-01 11:30:28 +09:00
errtoomanyrequests_test.go some fixes 2023-10-01 11:30:28 +09:00
errunauthorized_test.go some fixes 2023-10-01 11:30:28 +09:00
errunavailableforlegalreasons_test.go some fixes 2023-10-01 11:30:28 +09:00
errunprocessableentity_test.go some fixes 2023-10-01 11:30:28 +09:00
errunsupportedmediatype_test.go some fixes 2023-10-01 11:30:28 +09:00
errupgraderequired_test.go some fixes 2023-10-01 11:30:28 +09:00
errvariantalsonegotiates_test.go some fixes 2023-10-01 11:30:28 +09:00
expectationfailed.go some fixes 2023-10-01 11:30:28 +09:00
faileddependency.go some fixes 2023-10-01 11:30:28 +09:00
forbidden.go some fixes 2023-10-01 11:30:28 +09:00
gatewaytimeout.go some fixes 2023-10-01 11:30:28 +09:00
gone.go some fixes 2023-10-01 11:30:28 +09:00
httpversionnotsupported.go some fixes 2023-10-01 11:30:28 +09:00
insufficientstorage.go some fixes 2023-10-01 11:30:28 +09:00
internalservererror.go some fixes 2023-10-01 11:30:28 +09:00
lengthrequired.go some fixes 2023-10-01 11:30:28 +09:00
locked.go some fixes 2023-10-01 11:30:28 +09:00
loopdetected.go some fixes 2023-10-01 11:30:28 +09:00
methodnotallowed.go some fixes 2023-10-01 11:30:28 +09:00
misdirectedrequest.go some fixes 2023-10-01 11:30:28 +09:00
networkauthenticationrequired.go some fixes 2023-10-01 11:30:28 +09:00
notacceptable.go some fixes 2023-10-01 11:30:28 +09:00
notextended.go some fixes 2023-10-01 11:30:28 +09:00
notfound.go some fixes 2023-10-01 11:30:28 +09:00
notimplemented.go some fixes 2023-10-01 11:30:28 +09:00
paymentrequired.go some fixes 2023-10-01 11:30:28 +09:00
preconditionfailed.go some fixes 2023-10-01 11:30:28 +09:00
preconditionrequired.go some fixes 2023-10-01 11:30:28 +09:00
proxyauthrequired.go some fixes 2023-10-01 11:30:28 +09:00
requestedrangenotsatisfiable.go some fixes 2023-10-01 11:30:28 +09:00
requestentitytoolarge.go some fixes 2023-10-01 11:30:28 +09:00
requestheaderfieldstoolarge.go some fixes 2023-10-01 11:30:28 +09:00
requesttimeout.go some fixes 2023-10-01 11:30:28 +09:00
requesturitoolong.go some fixes 2023-10-01 11:30:28 +09:00
servererror.go client errors 2017-02-06 12:58:13 -08:00
serviceunavailable.go some fixes 2023-10-01 11:30:28 +09:00
teapot.go some fixes 2023-10-01 11:30:28 +09:00
tooearly.go some fixes 2023-10-01 11:30:28 +09:00
toomanyrequests.go some fixes 2023-10-01 11:30:28 +09:00
unauthorized.go some fixes 2023-10-01 11:30:28 +09:00
unavailableforlegalreasons.go some fixes 2023-10-01 11:30:28 +09:00
unprocessableentity.go some fixes 2023-10-01 11:30:28 +09:00
unsupportedmediatype.go some fixes 2023-10-01 11:30:28 +09:00
upgraderequired.go some fixes 2023-10-01 11:30:28 +09:00
variantalsonegotiates.go some fixes 2023-10-01 11:30:28 +09:00

README.md

go-errhttp

Package errhttp provides errors and types that make dealing with HTTP response errors easier, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/sourcecode.social/reiver/go-errhttp

GoDoc

Example

Here is an example of wrapping an error:

	import "sourcecode.social/reiver/go-errhttp"
	
	// ...
	
	if err := something(); nil != err {
		return errhttp.BadRequestWrap(err)
	}
	
	// ...
	
	switch err.(type) {
	case errhttp.BadRequest:
		//@TODO
	case errhttp.NotFound:
		//@TODO
	case errhttp.InternalServerError:
		//@TODO
		
	case errhttp.ClientError:
		//@TODO
	case errhttp.ServerError:
		//@TODO
		
	default:
		//@TODO
	}

Here is an example of using one of the package global variable errors:

	import "sourcecode.social/reiver/go-errhttp"
	
	// ...
	
	return errhttp.ErrBadRequest
	
	// ...
	
	switch err.(type) {
	case errhttp.BadRequest:
		//@TODO
	case errhttp.NotFound:
		//@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:

	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
	}