Error is a convenient way for a Handler to populate the essential fields of a HandlerError. If err is itself a HandlerError, then any essential fields that are not set will be populated.
(statusCode int, err error)
| 30 | // HandlerError, then any essential fields that are not |
| 31 | // set will be populated. |
| 32 | func Error(statusCode int, err error) HandlerError { |
| 33 | const idLen = 9 |
| 34 | var he HandlerError |
| 35 | if errors.As(err, &he) { |
| 36 | if he.ID == "" { |
| 37 | he.ID = randString(idLen, true) |
| 38 | } |
| 39 | if he.Trace == "" { |
| 40 | he.Trace = trace() |
| 41 | } |
| 42 | if he.StatusCode == 0 { |
| 43 | he.StatusCode = statusCode |
| 44 | } |
| 45 | return he |
| 46 | } |
| 47 | return HandlerError{ |
| 48 | ID: randString(idLen, true), |
| 49 | StatusCode: statusCode, |
| 50 | Err: err, |
| 51 | Trace: trace(), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // HandlerError is a serializable representation of |
| 56 | // an error from within an HTTP handler. |