OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package prpc | 5 package prpc |
6 | 6 |
7 import "fmt" | 7 import "fmt" |
8 | 8 |
9 // httpError is an error with HTTP status. | 9 // httpError is an error with HTTP status. |
10 // Many internal functions return *httpError instead of error | 10 // Many internal functions return *httpError instead of error |
11 // to guarantee expected HTTP status via type-system. | 11 // to guarantee expected HTTP status via type-system. |
12 // Supported by ErrorStatus and ErrorDesc. | 12 // Supported by ErrorStatus and ErrorDesc. |
13 type httpError struct { | 13 type httpError struct { |
14 err error | 14 err error |
15 status int | 15 status int |
16 } | 16 } |
17 | 17 |
18 func (e *httpError) Error() string { | 18 func (e *httpError) Error() string { |
19 return fmt.Sprintf("HTTP %d: %s", e.status, e.err) | 19 return fmt.Sprintf("HTTP %d: %s", e.status, e.err) |
20 } | 20 } |
21 | 21 |
| 22 func (e *httpError) InnerError() error { |
| 23 return e.err |
| 24 } |
| 25 |
22 // withStatus wraps an error with an HTTP status. | 26 // withStatus wraps an error with an HTTP status. |
23 // If err is nil, returns nil. | 27 // If err is nil, returns nil. |
24 func withStatus(err error, status int) *httpError { | 28 func withStatus(err error, status int) *httpError { |
25 if _, ok := err.(*httpError); ok { | 29 if _, ok := err.(*httpError); ok { |
26 panicf("httpError in httpError") | 30 panicf("httpError in httpError") |
27 } | 31 } |
28 if err == nil { | 32 if err == nil { |
29 return nil | 33 return nil |
30 } | 34 } |
31 return &httpError{err, status} | 35 return &httpError{err, status} |
32 } | 36 } |
33 | 37 |
34 // errorf creates a new error with an HTTP status. | 38 // errorf creates a new error with an HTTP status. |
35 func errorf(status int, format string, a ...interface{}) *httpError { | 39 func errorf(status int, format string, a ...interface{}) *httpError { |
36 return withStatus(fmt.Errorf(format, a...), status) | 40 return withStatus(fmt.Errorf(format, a...), status) |
37 } | 41 } |
38 | 42 |
39 // Errorf creates a new error with an HTTP status. | 43 // Errorf creates a new error with an HTTP status. |
40 // | 44 // |
41 // See also grpc.Errorf that accepts a gRPC code. | 45 // See also grpc.Errorf that accepts a gRPC code. |
42 func Errorf(status int, format string, a ...interface{}) error { | 46 func Errorf(status int, format string, a ...interface{}) error { |
43 return errorf(status, format, a...) | 47 return errorf(status, format, a...) |
44 } | 48 } |
45 | 49 |
46 func panicf(format string, a ...interface{}) { | 50 func panicf(format string, a ...interface{}) { |
47 panic(fmt.Errorf(format, a...)) | 51 panic(fmt.Errorf(format, a...)) |
48 } | 52 } |
OLD | NEW |