github.com/v2fly/tools@v0.100.0/internal/jsonrpc2_v2/wire.go (about)

     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  	// ErrUnknown should be used for all non coded errors.
    16  	ErrUnknown = NewError(-32001, "JSON RPC unknown error")
    17  	// ErrParse is used when invalid JSON was received by the server.
    18  	ErrParse = NewError(-32700, "JSON RPC parse error")
    19  	// ErrInvalidRequest is used when the JSON sent is not a valid Request object.
    20  	ErrInvalidRequest = NewError(-32600, "JSON RPC invalid request")
    21  	// ErrMethodNotFound should be returned by the handler when the method does
    22  	// not exist / is not available.
    23  	ErrMethodNotFound = NewError(-32601, "JSON RPC method not found")
    24  	// ErrInvalidParams should be returned by the handler when method
    25  	// parameter(s) were invalid.
    26  	ErrInvalidParams = NewError(-32602, "JSON RPC invalid params")
    27  	// ErrInternal indicates a failure to process a call correctly
    28  	ErrInternal = NewError(-32603, "JSON RPC internal error")
    29  
    30  	// The following errors are not part of the json specification, but
    31  	// compliant extensions specific to this implimentation.
    32  
    33  	// ErrServerOverloaded is returned when a message was refused due to a
    34  	// server being temporarily unable to accept any new messages.
    35  	ErrServerOverloaded = NewError(-32000, "JSON RPC overloaded")
    36  )
    37  
    38  const wireVersion = "2.0"
    39  
    40  // wireCombined has all the fields of both Request and Response.
    41  // We can decode this and then work out which it is.
    42  type wireCombined struct {
    43  	VersionTag string          `json:"jsonrpc"`
    44  	ID         interface{}     `json:"id,omitempty"`
    45  	Method     string          `json:"method,omitempty"`
    46  	Params     json.RawMessage `json:"params,omitempty"`
    47  	Result     json.RawMessage `json:"result,omitempty"`
    48  	Error      *wireError      `json:"error,omitempty"`
    49  }
    50  
    51  // wireError represents a structured error in a Response.
    52  type wireError struct {
    53  	// Code is an error code indicating the type of failure.
    54  	Code int64 `json:"code"`
    55  	// Message is a short description of the error.
    56  	Message string `json:"message"`
    57  	// Data is optional structured data containing additional information about the error.
    58  	Data json.RawMessage `json:"data,omitempty"`
    59  }
    60  
    61  // NewError returns an error that will encode on the wire correctly.
    62  // The standard codes are made available from this package, this function should
    63  // only be used to build errors for application specific codes as allowed by the
    64  // specification.
    65  func NewError(code int64, message string) error {
    66  	return &wireError{
    67  		Code:    code,
    68  		Message: message,
    69  	}
    70  }
    71  
    72  func (err *wireError) Error() string {
    73  	return err.Message
    74  }