2016-08-10 02:04:28 +00:00
|
|
|
package errors
|
|
|
|
|
|
2016-12-20 18:35:22 +00:00
|
|
|
import "net/http"
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2016-08-10 02:04:28 +00:00
|
|
|
// Kolide's internal representation for errors. It can be used to wrap another
|
|
|
|
|
// error (stored in Err), and additionally contains fields for public
|
|
|
|
|
// (PublicMessage) and private (PrivateMessage) error messages as well as the
|
2016-08-17 19:45:29 +00:00
|
|
|
// HTTP status code (StatusCode) corresponding to the error. Extra holds extra
|
|
|
|
|
// information that will be inserted as top level key/value pairs in the error
|
|
|
|
|
// response.
|
2016-08-10 02:04:28 +00:00
|
|
|
type KolideError struct {
|
|
|
|
|
Err error
|
|
|
|
|
StatusCode int
|
|
|
|
|
PublicMessage string
|
|
|
|
|
PrivateMessage string
|
2016-08-17 19:45:29 +00:00
|
|
|
Extra map[string]interface{}
|
2016-08-10 02:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implementation of error interface
|
|
|
|
|
func (e *KolideError) Error() string {
|
2016-09-29 04:21:39 +00:00
|
|
|
return e.PublicMessage
|
2016-08-10 02:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new KolideError specifying the public and private messages. The
|
|
|
|
|
// status code will be set to 500.
|
|
|
|
|
func New(publicMessage, privateMessage string) *KolideError {
|
|
|
|
|
return &KolideError{
|
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
|
PublicMessage: publicMessage,
|
|
|
|
|
PrivateMessage: privateMessage,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new KolideError specifying the HTTP status, and public and private
|
|
|
|
|
// messages.
|
|
|
|
|
func NewWithStatus(status int, publicMessage, privateMessage string) *KolideError {
|
|
|
|
|
return &KolideError{
|
|
|
|
|
StatusCode: status,
|
|
|
|
|
PublicMessage: publicMessage,
|
|
|
|
|
PrivateMessage: privateMessage,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new KolideError from an error type. The public message and status
|
|
|
|
|
// code should be specified, while the private message will be drawn from
|
|
|
|
|
// err.Error()
|
|
|
|
|
func NewFromError(err error, status int, publicMessage string) *KolideError {
|
|
|
|
|
return &KolideError{
|
|
|
|
|
Err: err,
|
|
|
|
|
StatusCode: status,
|
|
|
|
|
PublicMessage: publicMessage,
|
|
|
|
|
PrivateMessage: err.Error(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 19:20:29 +00:00
|
|
|
// Wrap a DB error with the extra KolideError decorations
|
2016-08-10 02:04:28 +00:00
|
|
|
func DatabaseError(err error) *KolideError {
|
2017-01-10 16:01:47 +00:00
|
|
|
return NewFromError(err, http.StatusInternalServerError, "Database error: "+err.Error())
|
2016-08-10 02:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 19:20:29 +00:00
|
|
|
// Wrap a server error with the extra KolideError decorations
|
|
|
|
|
func InternalServerError(err error) *KolideError {
|
|
|
|
|
return NewFromError(err, http.StatusInternalServerError, "Internal server error")
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-10 02:04:28 +00:00
|
|
|
// The status code returned for validation errors. Inspired by the Github API.
|
|
|
|
|
const StatusUnprocessableEntity = 422
|