hterrors

package module
v0.0.0-...-cd633df Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2022 License: MIT Imports: 10 Imported by: 0

README

go-hterrors

GoDoc

go get snai.pe/go-hterrors

A Go convenience library for handling non-2xx HTTP error codes.

The library does a best-effort parsing of the response body based on common MIME-types to produce a reasonable Go error.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(resp *http.Response, err error) (*http.Response, error)

Check is a convenience wrapper over CheckStatus -- if the passed error is non-nil, it is returned; otherwise, CheckStatus(resp) is returned.

This function exists to make it easier to write error handling code, by directly taking the expression that makes the request. For instance:

Check(http.Get("http://example.com"))

Check(http.Do(request))
Example
package main

import (
	"fmt"
	"net/http"

	"snai.pe/go-hterrors"
)

func main() {
	resp, err := hterrors.Check(http.Get("http://google.com"))
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(http.StatusText(resp.StatusCode))
}
Output:
OK
Example (Notfound)
package main

import (
	"fmt"
	"net/http"

	"snai.pe/go-hterrors"
)

func main() {
	resp, err := hterrors.Check(http.Get("http://google.com/invalid"))
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(http.StatusText(resp.StatusCode))
}
Output:
GET "http://google.com/invalid": Error 404 (Not Found)!!1 : 404. That’s an error. : The requested URL /invalid was not found on this server. That’s all we know.

func CheckResponse

func CheckResponse(resp *http.Response, checker ResponseChecker) error

CheckResponse returns an error if the response is rejected by the specified response checker. The returned error contains a digested version of the response body, and the response body is consumed.

func CheckStatus

func CheckStatus(resp *http.Response) error

CheckStatus returns an error if the status code of the specified response is not in the 2xx family. The returned error contains a digested version of the response body, and the reponse body is consumed.

It is a convenience wrapper over CheckResponse(resp, DefaultResponseChecker).

func CheckStatusOneOf

func CheckStatusOneOf(resp *http.Response, expectedStatuses ...int) error

CheckStatusOneOf returns an error if the status code of the specified response is not one of the expected statuses passed to this function. The returned error contains a digested version of the response body, and the reponse body is consumed.

func DefaultResponseChecker

func DefaultResponseChecker(resp *http.Response) bool

DefaultResponseChecker is the default ResponseChecker. It returns true if the status is 2xx.

Types

type ResponseChecker

type ResponseChecker func(*http.Response) bool

ResponseChecker represents a function that accepts or rejects a response based off some criteria. If rejected, the response is considered failed by the CheckReponse function below and and error is returned.

type StatusError

type StatusError struct {
	StatusCode int
	Message    string
}

StatusError represents a non-2xx HTTP status code, and the associated message returned by the server, if any.

func (*StatusError) Error

func (err *StatusError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址