You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.0 KiB
58 lines
1.0 KiB
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
)
|
|
|
|
// NewHTTPError
|
|
//
|
|
// @Description:
|
|
// @param code
|
|
// @param data
|
|
// @param detail
|
|
// @return *HTTPError
|
|
func NewHTTPError(code int, data interface{}, detail string) *HTTPError {
|
|
return &HTTPError{
|
|
Code: code,
|
|
Data: data,
|
|
Message: detail,
|
|
}
|
|
}
|
|
|
|
// HTTPError
|
|
// @Description:
|
|
type HTTPError struct {
|
|
Message string `json:"message"`
|
|
Code int `json:"code"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
// Error
|
|
//
|
|
// @Description:
|
|
// @receiver e
|
|
// @return string
|
|
func (e *HTTPError) Error() string {
|
|
return fmt.Sprintf("HTTPError: %d", e.Code)
|
|
}
|
|
|
|
// FromError
|
|
//
|
|
// @Description:
|
|
// @param err
|
|
// @return *HTTPError
|
|
func FromError(err error) *HTTPError {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if se := new(HTTPError); errors.As(err, &se) {
|
|
return se
|
|
}
|
|
if se := new(errors.Error); errors.As(err, &se) {
|
|
return NewHTTPError(int(se.Code), nil, se.Message)
|
|
}
|
|
return NewHTTPError(http.StatusBadRequest, new(struct{}), "error")
|
|
}
|
|
|