PageRenderTime 33ms CodeModel.GetById 25ms app.highlight 5ms RepoModel.GetById 2ms 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
 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
15package circuit
16
17import (
18	"encoding/gob"
19	"errors"
20	"fmt"
21)
22
23func init() {
24	gob.Register(&errorString{})
25	gob.Register(errors.New(""))
26}
27
28// NewError creates a simple text-based error that is registered with package
29// encoding/gob and therefore can be used in places of error interfaces during
30// cross-calls. In contrast, note that due to the rules of gob encoding error objects
31// that are not explicitly registered with gob cannot be assigned to error interfaces
32// that are to be gob-serialized during a cross-call.
33func NewError(fmt_ string, arg_ ...interface{}) error {
34	return &errorString{fmt.Sprintf(fmt_, arg_...)}
35}
36
37// FlattenError converts any error into a gob-serializable one that can be used in cross-calls.
38func FlattenError(err error) error {
39	if err == nil {
40		return nil
41	}
42	return NewError(err.Error())
43}
44
45type errorString struct {
46	S string
47}
48
49func (e *errorString) Error() string {
50	return e.S
51}