|
| 1 | +// Copyright (C) 2015-2024 The Neo Project. |
| 2 | +// |
| 3 | +// RpcError.cs file belongs to the neo project and is free |
| 4 | +// software distributed under the MIT software license, see the |
| 5 | +// accompanying file LICENSE in the main directory of the |
| 6 | +// repository or http://www.opensource.org/licenses/mit-license.php |
| 7 | +// for more details. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms with or without |
| 10 | +// modifications are permitted. |
| 11 | + |
| 12 | +using Neo.Json; |
| 13 | + |
| 14 | +namespace Neo.Plugins |
| 15 | +{ |
| 16 | + public class RpcError |
| 17 | + { |
| 18 | + #region Default Values |
| 19 | + |
| 20 | + // https://www.jsonrpc.org/specification |
| 21 | + // | code | message | meaning | |
| 22 | + // |--------------------|-----------------|-----------------------------------------------------------------------------------| |
| 23 | + // | -32700 | Parse error | Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text. | |
| 24 | + // | -32600 | Invalid request | The JSON sent is not a valid Request object. | |
| 25 | + // | -32601 | Method not found| The method does not exist / is not available. | |
| 26 | + // | -32602 | Invalid params | Invalid method parameter(s). | |
| 27 | + // | -32603 | Internal error | Internal JSON-RPC error. | |
| 28 | + // | -32000 to -32099 | Server error | Reserved for implementation-defined server-errors. | |
| 29 | + public static readonly RpcError InvalidRequest = new(-32600, "Invalid request"); |
| 30 | + public static readonly RpcError MethodNotFound = new(-32601, "Method not found"); |
| 31 | + public static readonly RpcError InvalidParams = new(-32602, "Invalid params"); |
| 32 | + public static readonly RpcError InternalServerError = new(-32603, "Internal server RpcError"); |
| 33 | + public static readonly RpcError BadRequest = new(-32700, "Bad request"); |
| 34 | + |
| 35 | + // https://github.com/neo-project/proposals/pull/156/files |
| 36 | + public static readonly RpcError UnknownBlock = new(-101, "Unknown block"); |
| 37 | + public static readonly RpcError UnknownContract = new(-102, "Unknown contract"); |
| 38 | + public static readonly RpcError UnknownTransaction = new(-103, "Unknown transaction"); |
| 39 | + public static readonly RpcError UnknownStorageItem = new(-104, "Unknown storage item"); |
| 40 | + public static readonly RpcError UnknownScriptContainer = new(-105, "Unknown script container"); |
| 41 | + public static readonly RpcError UnknownStateRoot = new(-106, "Unknown state root"); |
| 42 | + public static readonly RpcError UnknownSession = new(-107, "Unknown session"); |
| 43 | + public static readonly RpcError UnknownIterator = new(-108, "Unknown iterator"); |
| 44 | + public static readonly RpcError UnknownHeight = new(-109, "Unknown height"); |
| 45 | + |
| 46 | + public static readonly RpcError InsufficientFundsWallet = new(-300, "Insufficient funds in wallet"); |
| 47 | + public static readonly RpcError WalletFeeLimit = new(-301, "Wallet fee limit exceeded", "The necessary fee is more than the Max_fee, this transaction is failed. Please increase your Max_fee value."); |
| 48 | + public static readonly RpcError NoOpenedWallet = new(-302, "No opened wallet"); |
| 49 | + public static readonly RpcError WalletNotFound = new(-303, "Wallet not found"); |
| 50 | + public static readonly RpcError WalletNotSupported = new(-304, "Wallet not supported"); |
| 51 | + |
| 52 | + public static readonly RpcError VerificationFailed = new(-500, "Inventory verification failed"); |
| 53 | + public static readonly RpcError AlreadyExists = new(-501, "Inventory already exists"); |
| 54 | + public static readonly RpcError MempoolCapReached = new(-502, "Memory pool capacity reached"); |
| 55 | + public static readonly RpcError AlreadyInPool = new(-503, "Already in pool"); |
| 56 | + public static readonly RpcError InsufficientNetworkFee = new(-504, "Insufficient network fee"); |
| 57 | + public static readonly RpcError PolicyFailed = new(-505, "Policy check failed"); |
| 58 | + public static readonly RpcError InvalidScript = new(-509, "Invalid transaction script"); |
| 59 | + public static readonly RpcError InvalidAttribute = new(-507, "Invalid transaction attribute"); |
| 60 | + public static readonly RpcError InvalidSignature = new(-508, "Invalid signature"); |
| 61 | + public static readonly RpcError InvalidSize = new(-509, "Invalid inventory size"); |
| 62 | + public static readonly RpcError ExpiredTransaction = new(-510, "Expired transaction"); |
| 63 | + public static readonly RpcError InsufficientFunds = new(-511, "Insufficient funds for fee"); |
| 64 | + public static readonly RpcError InvalidContractVerification = new(-512, "Invalid contract verification function"); |
| 65 | + |
| 66 | + public static readonly RpcError AccessDenied = new(-600, "Access denied"); |
| 67 | + public static readonly RpcError SessionsDisabled = new(-601, "State iterator sessions disabled"); |
| 68 | + public static readonly RpcError OracleDisabled = new(-602, "Oracle service disabled"); |
| 69 | + public static readonly RpcError OracleRequestFinished = new(-603, "Oracle request already finished"); |
| 70 | + public static readonly RpcError OracleRequestNotFound = new(-604, "Oracle request not found"); |
| 71 | + public static readonly RpcError OracleNotDesignatedNode = new(-605, "Not a designated oracle node"); |
| 72 | + public static readonly RpcError UnsupportedState = new(-606, "Old state not supported"); |
| 73 | + public static readonly RpcError InvalidProof = new(-607, "Invalid state proof"); |
| 74 | + public static readonly RpcError ExecutionFailed = new(-608, "Contract execution failed"); |
| 75 | + |
| 76 | + #endregion |
| 77 | + |
| 78 | + public int Code { get; set; } |
| 79 | + public string Message { get; set; } |
| 80 | + public string Data { get; set; } |
| 81 | + |
| 82 | + public RpcError(int code, string message, string data = null) |
| 83 | + { |
| 84 | + Code = code; |
| 85 | + Message = message; |
| 86 | + Data = data; |
| 87 | + } |
| 88 | + |
| 89 | + public override string ToString() => string.IsNullOrEmpty(Data) ? $"{Message} ({Code})" : $"{Message} ({Code}) - {Data}"; |
| 90 | + |
| 91 | + public JToken ToJson() |
| 92 | + { |
| 93 | + JObject json = new(); |
| 94 | + json["code"] = Code; |
| 95 | + json["message"] = ErrorMessage; |
| 96 | + if (!string.IsNullOrEmpty(Data)) |
| 97 | + json["data"] = Data; |
| 98 | + return json; |
| 99 | + } |
| 100 | + |
| 101 | + public string ErrorMessage => string.IsNullOrEmpty(Data) ? $"{Message}" : $"{Message} - {Data}"; |
| 102 | + } |
| 103 | +} |
0 commit comments