80 lines
1.9 KiB
Go
Raw Permalink Normal View History

2022-08-26 14:18:35 +03:00
package agdhttp
import (
"fmt"
"net/http"
"github.com/AdguardTeam/golibs/errors"
2023-06-11 12:58:40 +03:00
"github.com/AdguardTeam/golibs/httphdr"
2022-08-26 14:18:35 +03:00
)
// Common HTTP Errors
// StatusError is returned by methods when the HTTP status code is different
// from the expected.
type StatusError struct {
ServerName string
Expected int
Got int
}
// type check
var _ error = (*StatusError)(nil)
// Error implements the error interface for *StatusError.
func (err *StatusError) Error() (msg string) {
return fmt.Sprintf(
"server %q: status code error: expected %d, got %d",
err.ServerName,
err.Expected,
err.Got,
)
}
// CheckStatus returns a non-nil error with the data from resp if the status
// code in resp is not equal to expected. resp must be non-nil.
//
// Any error returned will have the underlying type of *StatusError.
func CheckStatus(resp *http.Response, expected int) (err error) {
if resp.StatusCode == expected {
return nil
}
return &StatusError{
2023-06-11 12:58:40 +03:00
ServerName: resp.Header.Get(httphdr.Server),
2022-08-26 14:18:35 +03:00
Expected: expected,
Got: resp.StatusCode,
}
}
// ServerError is returned as general error in case header Server was specified.
type ServerError struct {
Err error
ServerName string
}
// type check
var _ error = (*ServerError)(nil)
// Error implements the error interface for *ServerError.
func (err *ServerError) Error() (msg string) {
return fmt.Sprintf("server %q: %s", err.ServerName, err.Err)
}
// type check
var _ errors.Wrapper = (*ServerError)(nil)
// Unwrap implements the errors.Wrapper interface for *ServerError.
func (err *ServerError) Unwrap() (unwrapped error) {
return err.Err
}
// WrapServerError wraps err inside a *ServerError including data from resp.
// resp must not be nil.
func WrapServerError(err error, resp *http.Response) (wrapped *ServerError) {
return &ServerError{
Err: err,
2023-06-11 12:58:40 +03:00
ServerName: resp.Header.Get(httphdr.Server),
2022-08-26 14:18:35 +03:00
}
}