-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
52 lines (46 loc) · 1 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package node2go
import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
)
// General errors
var (
ErrMalformedMessage = errors.New(
"message must constist of a function name to be called, colon and json body",
)
ErrFuncNotImplemented = func(name string) error {
return fmt.Errorf("handler for `%s` not implemented", name)
}
)
// Err is a wrapper for error, has an id of incoming message and embeds
// the connection it originated in
type Err struct {
net.Conn `json:"-"`
id []byte
error error
}
// MarshalJSON implements the marshaller interface and returns json with a
// single error field
func (e *Err) MarshalJSON() ([]byte, error) {
type Alias Err
return json.Marshal(&struct {
Error string `json:"error"`
*Alias
}{
Error: e.error.Error(),
Alias: (*Alias)(e),
})
}
func (e *Err) json() []byte {
j, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
return formatResponse(j, e.id)
}
func newErr(conn net.Conn, id []byte, err error) *Err {
return &Err{conn, id, err}
}