github.com/amazechain/amc@v0.1.3/modules/rpc/jsonrpc/errors.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package jsonrpc
    18  
    19  import "fmt"
    20  
    21  type HTTPError struct {
    22  	StatusCode int
    23  	Status     string
    24  	Body       []byte
    25  }
    26  
    27  func (err HTTPError) Error() string {
    28  	if len(err.Body) == 0 {
    29  		return err.Status
    30  	}
    31  	return fmt.Sprintf("%v: %s", err.Status, err.Body)
    32  }
    33  
    34  type Error interface {
    35  	Error() string
    36  	ErrorCode() int
    37  }
    38  
    39  type DataError interface {
    40  	Error() string
    41  	ErrorData() interface{}
    42  }
    43  
    44  var (
    45  	_ Error = new(methodNotFoundError)
    46  	_ Error = new(subscriptionNotFoundError)
    47  	_ Error = new(parseError)
    48  	_ Error = new(invalidRequestError)
    49  	_ Error = new(invalidMessageError)
    50  	_ Error = new(invalidParamsError)
    51  )
    52  
    53  const defaultErrorCode = -32000
    54  
    55  type methodNotFoundError struct{ method string }
    56  
    57  func (e *methodNotFoundError) ErrorCode() int { return -32601 }
    58  
    59  func (e *methodNotFoundError) Error() string {
    60  	return fmt.Sprintf("the method %s does not exist/is not available", e.method)
    61  }
    62  
    63  type subscriptionNotFoundError struct{ namespace, subscription string }
    64  
    65  func (e *subscriptionNotFoundError) ErrorCode() int { return -32601 }
    66  
    67  func (e *subscriptionNotFoundError) Error() string {
    68  	return fmt.Sprintf("no %q subscription in %s namespace", e.subscription, e.namespace)
    69  }
    70  
    71  type parseError struct{ message string }
    72  
    73  func (e *parseError) ErrorCode() int { return -32700 }
    74  
    75  func (e *parseError) Error() string { return e.message }
    76  
    77  type invalidRequestError struct{ message string }
    78  
    79  func (e *invalidRequestError) ErrorCode() int { return -32600 }
    80  
    81  func (e *invalidRequestError) Error() string { return e.message }
    82  
    83  type invalidMessageError struct{ message string }
    84  
    85  func (e *invalidMessageError) ErrorCode() int { return -32700 }
    86  
    87  func (e *invalidMessageError) Error() string { return e.message }
    88  
    89  type invalidParamsError struct{ message string }
    90  
    91  func (e *invalidParamsError) ErrorCode() int { return -32602 }
    92  
    93  func (e *invalidParamsError) Error() string { return e.message }