2017-02-06 20:57:57 +00:00
|
|
|
# go-errhttp
|
|
|
|
|
2023-08-14 12:56:20 +00:00
|
|
|
Package **errhttp** provides errors and types that make dealing with HTTP response errors easier, for the Go programming language.
|
2017-02-06 20:57:57 +00:00
|
|
|
|
2023-08-14 12:57:44 +00:00
|
|
|
## Documention
|
|
|
|
|
|
|
|
Online documentation, which includes examples, can be found at: http://godoc.org/sourcecode.social/reiver/go-errhttp
|
|
|
|
|
|
|
|
[![GoDoc](https://godoc.org/sourcecode.social/reiver/go-errhttp?status.svg)](https://godoc.org/sourcecode.social/reiver/go-errhttp)
|
2017-02-06 20:57:57 +00:00
|
|
|
|
2017-02-06 21:04:29 +00:00
|
|
|
## Example
|
2023-08-14 12:56:20 +00:00
|
|
|
|
|
|
|
Here is an example of wrapping an error:
|
|
|
|
|
2017-02-06 21:04:29 +00:00
|
|
|
```go
|
2023-08-14 12:56:20 +00:00
|
|
|
import "sourcecode.social/reiver/go-errhttp"
|
2017-02-06 21:04:29 +00:00
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
if err := something(); nil != err {
|
|
|
|
return errhttp.BadRequestWrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case errhttp.BadRequest:
|
|
|
|
//@TODO
|
|
|
|
case errhttp.NotFound:
|
|
|
|
//@TODO
|
2019-07-15 19:31:32 +00:00
|
|
|
case errhttp.InternalServerError:
|
2017-02-06 21:04:29 +00:00
|
|
|
//@TODO
|
2023-08-14 13:56:09 +00:00
|
|
|
|
2019-07-15 19:31:32 +00:00
|
|
|
case errhttp.ClientError:
|
2017-02-06 21:04:29 +00:00
|
|
|
//@TODO
|
2019-07-15 19:31:32 +00:00
|
|
|
case errhttp.ServerError:
|
2017-02-06 21:04:29 +00:00
|
|
|
//@TODO
|
2023-08-14 13:56:09 +00:00
|
|
|
|
2017-02-06 21:04:29 +00:00
|
|
|
default:
|
|
|
|
//@TODO
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-08-14 12:56:20 +00:00
|
|
|
Here is an example of using one of the package global variable errors:
|
|
|
|
|
|
|
|
```go
|
|
|
|
import "sourcecode.social/reiver/go-errhttp"
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
return errhttp.ErrBadRequest
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case errhttp.BadRequest:
|
|
|
|
//@TODO
|
|
|
|
case errhttp.NotFound:
|
|
|
|
//@TODO
|
|
|
|
case errhttp.InternalServerError:
|
|
|
|
//@TODO
|
2023-08-14 13:56:09 +00:00
|
|
|
|
2023-08-14 12:56:20 +00:00
|
|
|
case errhttp.ClientError:
|
|
|
|
//@TODO
|
|
|
|
case errhttp.ServerError:
|
|
|
|
//@TODO
|
2023-08-14 13:56:09 +00:00
|
|
|
|
|
|
|
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
|
2023-08-14 12:56:20 +00:00
|
|
|
|
2023-08-14 13:56:09 +00:00
|
|
|
// ...
|
|
|
|
|
|
|
|
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
|
2023-08-14 12:56:20 +00:00
|
|
|
default:
|
|
|
|
//@TODO
|
|
|
|
}
|
|
|
|
```
|