dashboard-v2/app/errors.go
2024-12-01 11:15:24 +00:00

41 lines
986 B
Go

package app
import (
"errors"
"fmt"
"github.com/rxdn/gdl/rest/request"
)
type ApiError struct {
InternalError error
ExternalMessage string
stacktrace error
}
var _ error = (*ApiError)(nil)
func NewError(internalError error, externalMessage string) *ApiError {
return &ApiError{
InternalError: internalError,
ExternalMessage: externalMessage,
}
}
func NewServerError(internalError error) *ApiError {
return NewError(internalError, "An internal server error occurred")
}
func (e *ApiError) Error() string {
var restError request.RestError
if errors.As(e.InternalError, &restError) {
return fmt.Sprintf("internal error: %v, external message: %s, rest error: Discord returned HTTP %d: %s",
e.InternalError, e.ExternalMessage, restError.StatusCode, restError.ApiError.Message)
} else {
return fmt.Sprintf("internal error: %v, external message: %s", e.InternalError, e.ExternalMessage)
}
}
func (e *ApiError) Unwrap() error {
return e.InternalError
}