Debian Codegraph @

internal/jsonrpc2_v2/wire.go

raw symbols SCIP
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package jsonrpc2
6
7 import (
8 "encoding/json"
9 )
10
11 // This file contains the go forms of the wire specification.
12 // see http://www.jsonrpc.org/specification for details
13
14 var (
15 // ErrParse is used when invalid JSON was received by the server.
16 ErrParse = NewError(-32700, "JSON RPC parse error")
17 // ErrInvalidRequest is used when the JSON sent is not a valid Request object.
18 ErrInvalidRequest = NewError(-32600, "JSON RPC invalid request")
19 // ErrMethodNotFound should be returned by the handler when the method does
20 // not exist / is not available.
21 ErrMethodNotFound = NewError(-32601, "JSON RPC method not found")
22 // ErrInvalidParams should be returned by the handler when method
23 // parameter(s) were invalid.
24 ErrInvalidParams = NewError(-32602, "JSON RPC invalid params")
25 // ErrInternal indicates a failure to process a call correctly
26 ErrInternal = NewError(-32603, "JSON RPC internal error")
27
28 // The following errors are not part of the json specification, but
29 // compliant extensions specific to this implementation.
30
31 // ErrServerOverloaded is returned when a message was refused due to a
32 // server being temporarily unable to accept any new messages.
33 ErrServerOverloaded = NewError(-32000, "JSON RPC overloaded")
34 // ErrUnknown should be used for all non coded errors.
35 ErrUnknown = NewError(-32001, "JSON RPC unknown error")
36 // ErrServerClosing is returned for calls that arrive while the server is closing.
37 ErrServerClosing = NewError(-32002, "JSON RPC server is closing")
38 // ErrClientClosing is a dummy error returned for calls initiated while the client is closing.
39 ErrClientClosing = NewError(-32003, "JSON RPC client is closing")
40 )
41
42 const wireVersion = "2.0"
43
44 // wireCombined has all the fields of both Request and Response.
45 // We can decode this and then work out which it is.
46 type wireCombined struct {
47 VersionTag string `json:"jsonrpc"`
48 ID any `json:"id,omitempty"`
49 Method string `json:"method,omitempty"`
50 Params json.RawMessage `json:"params,omitempty"`
51 Result json.RawMessage `json:"result,omitempty"`
52 Error *WireError `json:"error,omitempty"`
53 }
54
55 // WireError represents a structured error in a Response.
56 type WireError struct {
57 // Code is an error code indicating the type of failure.
58 Code int64 `json:"code"`
59 // Message is a short description of the error.
60 Message string `json:"message"`
61 // Data is optional structured data containing additional information about the error.
62 Data json.RawMessage `json:"data,omitempty"`
63 }
64
65 // NewError returns an error that will encode on the wire correctly.
66 // The standard codes are made available from this package, this function should
67 // only be used to build errors for application specific codes as allowed by the
68 // specification.
69 func NewError(code int64, message string) error {
70 return &WireError{
71 Code: code,
72 Message: message,
73 }
74 }
75
76 func (err *WireError) Error() string {
77 return err.Message
78 }
79
80 func (err *WireError) Is(other error) bool {
81 w, ok := other.(*WireError)
82 if !ok {
83 return false
84 }
85 return err.Code == w.Code
86 }