PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/circuit/use/circuit/error.go

https://github.com/Web5design/gocircuit
Go | 51 lines | 25 code | 7 blank | 19 comment | 2 complexity | 3051dafab976a564d09497fa0472bb7f MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright 2013 Tumblr, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package circuit
  15. import (
  16. "encoding/gob"
  17. "errors"
  18. "fmt"
  19. )
  20. func init() {
  21. gob.Register(&errorString{})
  22. gob.Register(errors.New(""))
  23. }
  24. // NewError creates a simple text-based error that is registered with package
  25. // encoding/gob and therefore can be used in places of error interfaces during
  26. // cross-calls. In contrast, note that due to the rules of gob encoding error objects
  27. // that are not explicitly registered with gob cannot be assigned to error interfaces
  28. // that are to be gob-serialized during a cross-call.
  29. func NewError(fmt_ string, arg_ ...interface{}) error {
  30. return &errorString{fmt.Sprintf(fmt_, arg_...)}
  31. }
  32. // FlattenError converts any error into a gob-serializable one that can be used in cross-calls.
  33. func FlattenError(err error) error {
  34. if err == nil {
  35. return nil
  36. }
  37. return NewError(err.Error())
  38. }
  39. type errorString struct {
  40. S string
  41. }
  42. func (e *errorString) Error() string {
  43. return e.S
  44. }