Chromium Code Reviews| 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 ( | 
| 8 » "fmt" | |
| 9 ) | |
| 8 | 10 | 
| 9 // httpError is an error with HTTP status. | 11 // protocolError is returned if a pRPC request is malformed. | 
| 10 // Many internal functions return *httpError instead of error | 12 type protocolError struct { | 
| 11 // to guarantee expected HTTP status via type-system. | |
| 12 // Supported by ErrorStatus and ErrorDesc. | |
| 13 type httpError struct { | |
| 14 err error | 13 err error | 
| 15 » status int | 14 » status int // HTTP status to use in response. | 
| 16 } | 15 } | 
| 17 | 16 | 
| 18 func (e *httpError) Error() string { | 17 func (e *protocolError) Error() string { | 
| 19 » return fmt.Sprintf("HTTP %d: %s", e.status, e.err) | 18 » return fmt.Sprintf("pRPC: %s", e.err) | 
| 20 } | 19 } | 
| 21 | 20 | 
| 22 // withStatus wraps an error with an HTTP status. | 21 // withStatus wraps an error with an HTTP status. | 
| 23 // If err is nil, returns nil. | 22 // If err is nil, returns nil. | 
| 24 func withStatus(err error, status int) *httpError { | 23 func withStatus(err error, status int) *protocolError { | 
| 25 » if _, ok := err.(*httpError); ok { | 24 » if _, ok := err.(*protocolError); ok { | 
| 26 panicf("httpError in httpError") | 25 panicf("httpError in httpError") | 
| 27 } | 26 } | 
| 28 if err == nil { | 27 if err == nil { | 
| 29 return nil | 28 return nil | 
| 30 } | 29 } | 
| 31 » return &httpError{err, status} | 30 » return &protocolError{err, status} | 
| 32 } | 31 } | 
| 33 | 32 | 
| 34 // errorf creates a new error with an HTTP status. | 33 // errorf creates a new protocol error. | 
| 35 func errorf(status int, format string, a ...interface{}) *httpError { | 34 func errorf(status int, format string, a ...interface{}) *protocolError { | 
| 36 return withStatus(fmt.Errorf(format, a...), status) | 35 return withStatus(fmt.Errorf(format, a...), status) | 
| 37 } | 36 } | 
| 38 | 37 | 
| 39 // Errorf creates a new error with an HTTP status. | |
| 40 // | |
| 41 // See also grpc.Errorf that accepts a gRPC code. | |
| 42 func Errorf(status int, format string, a ...interface{}) error { | |
| 43 return errorf(status, format, a...) | |
| 44 } | |
| 45 | |
| 46 func panicf(format string, a ...interface{}) { | 38 func panicf(format string, a ...interface{}) { | 
| 47 panic(fmt.Errorf(format, a...)) | 39 panic(fmt.Errorf(format, a...)) | 
| 48 } | 40 } | 
| 41 | |
| 42 func assert(condition bool) { | |
| 
 
dnj (Google)
2016/01/26 16:13:56
Can we make an effort to get rid of all instances
 
nodir
2016/01/26 18:01:07
I don't panic unless there is an obvious bug in se
 
dnj
2016/01/26 18:50:27
Yeah it's a tough line to draw. A panic will compl
 
 | |
| 43 if !condition { | |
| 44 panicf("assertion failed") | |
| 45 } | |
| 46 } | |
| OLD | NEW |