Files
hatchet/pkg/errors/errors.go
Alexander Belanger 366c79441d first commit
2023-12-15 13:08:04 -05:00

50 lines
1.1 KiB
Go

package errors
import (
"fmt"
)
type DetailedError struct {
// a custom Hatchet error code
// example: 1400
Code uint `json:"code"`
// a reason for this error
Reason string `json:"reason"`
// a description for this error
// example: A descriptive error message
Description string `json:"description"`
// a link to the documentation for this error, if it exists
// example: github.com/hatchet-dev/hatchet
DocsLink string `json:"docs_link"`
}
func (e DetailedError) Error() string {
errStr := fmt.Sprintf("error %d: %s", e.Code, e.Description)
if e.DocsLink != "" {
errStr = fmt.Sprintf("%s, see %s", errStr, e.DocsLink)
}
return errStr
}
func NewError(code uint, reason, description, docsLink string) *DetailedError {
return &DetailedError{
Code: code,
Reason: reason,
Description: description,
DocsLink: docsLink,
}
}
func NewErrInternal(err error) *DetailedError {
return NewError(500, "Internal Server Error", err.Error(), "")
}
func NewErrForbidden(err error) *DetailedError {
return NewError(403, "Forbidden", err.Error(), "")
}