-
Notifications
You must be signed in to change notification settings - Fork 6
/
errors.go
52 lines (43 loc) · 1.29 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package errors
import "net/http"
// ErrorType describes the datastore error
type ErrorType string
// BadParameter represents a bad parameter provided by the user
var BadParameter ErrorType = "BAD_PARAM"
// NotFound represents an error in which the record could not be found
var NotFound ErrorType = "NOT_FOUND"
// UnknownError ... something unknown occured
var UnknownError ErrorType = "UNKNOWN"
// New returns a pointer to a new DatastoreError. If the `err` parameter is `nil`, this function will return `nil` so
// errors can be checked against `nil`
func New(typ ErrorType, err error) *DatastoreError {
if err != nil {
return &DatastoreError{
errorType: typ,
err: err,
}
}
return nil
}
// DatastoreError implements Error
type DatastoreError struct {
errorType ErrorType
err error
}
// Code attempts to translate the datastore error to a HTTP status code
func (e *DatastoreError) Code() int {
switch e.errorType {
case BadParameter:
return http.StatusBadRequest
case NotFound:
return http.StatusNotFound
case UnknownError:
return http.StatusInternalServerError
default:
return http.StatusInternalServerError
}
}
// Error returns the error as a string (in order to implement the built-in error interface)
func (e *DatastoreError) Error() string {
return e.err.Error()
}