github.com/goplus/gop@v1.2.6/x/jsonrpc2/wire.go (about) 1 /* 2 * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Copyright 2018 The Go Authors. All rights reserved. 18 // Use of this source code is governed by a BSD-style 19 // license that can be found in the LICENSE file. 20 21 package jsonrpc2 22 23 import ( 24 "encoding/json" 25 ) 26 27 // This file contains the go forms of the wire specification. 28 // see http://www.jsonrpc.org/specification for details 29 30 var ( 31 // ErrParse is used when invalid JSON was received by the server. 32 ErrParse = NewError(-32700, "JSON RPC parse error") 33 // ErrInvalidRequest is used when the JSON sent is not a valid Request object. 34 ErrInvalidRequest = NewError(-32600, "JSON RPC invalid request") 35 // ErrMethodNotFound should be returned by the handler when the method does 36 // not exist / is not available. 37 ErrMethodNotFound = NewError(-32601, "JSON RPC method not found") 38 // ErrInvalidParams should be returned by the handler when method 39 // parameter(s) were invalid. 40 ErrInvalidParams = NewError(-32602, "JSON RPC invalid params") 41 // ErrInternal indicates a failure to process a call correctly 42 ErrInternal = NewError(-32603, "JSON RPC internal error") 43 44 // The following errors are not part of the json specification, but 45 // compliant extensions specific to this implementation. 46 47 // ErrServerOverloaded is returned when a message was refused due to a 48 // server being temporarily unable to accept any new messages. 49 ErrServerOverloaded = NewError(-32000, "JSON RPC overloaded") 50 // ErrUnknown should be used for all non coded errors. 51 ErrUnknown = NewError(-32001, "JSON RPC unknown error") 52 // ErrServerClosing is returned for calls that arrive while the server is closing. 53 ErrServerClosing = NewError(-32002, "JSON RPC server is closing") 54 // ErrClientClosing is a dummy error returned for calls initiated while the client is closing. 55 ErrClientClosing = NewError(-32003, "JSON RPC client is closing") 56 ) 57 58 const wireVersion = "2.0" 59 60 // wireCombined has all the fields of both Request and Response. 61 // We can decode this and then work out which it is. 62 type wireCombined struct { 63 VersionTag string `json:"jsonrpc"` 64 ID interface{} `json:"id,omitempty"` 65 Method string `json:"method,omitempty"` 66 Params json.RawMessage `json:"params,omitempty"` 67 Result json.RawMessage `json:"result,omitempty"` 68 Error *wireError `json:"error,omitempty"` 69 } 70 71 // wireError represents a structured error in a Response. 72 type wireError struct { 73 // Code is an error code indicating the type of failure. 74 Code int64 `json:"code"` 75 // Message is a short description of the error. 76 Message string `json:"message"` 77 // Data is optional structured data containing additional information about the error. 78 Data json.RawMessage `json:"data,omitempty"` 79 } 80 81 // NewError returns an error that will encode on the wire correctly. 82 // The standard codes are made available from this package, this function should 83 // only be used to build errors for application specific codes as allowed by the 84 // specification. 85 func NewError(code int64, message string) error { 86 return &wireError{ 87 Code: code, 88 Message: message, 89 } 90 } 91 92 func (err *wireError) Error() string { 93 return err.Message 94 } 95 96 func (err *wireError) Is(other error) bool { 97 w, ok := other.(*wireError) 98 if !ok { 99 return false 100 } 101 return err.Code == w.Code 102 }