|
| 1 | +""" |
| 2 | + JSONRPCError |
| 3 | +
|
| 4 | +An object representing a JSON-RPC Error. |
| 5 | +
|
| 6 | +Fields: |
| 7 | + * code::Int |
| 8 | + * msg::AbstractString |
| 9 | + * data::Any |
| 10 | +
|
| 11 | +See Section 5.1 of the JSON RPC 2.0 specification for more information. |
| 12 | +""" |
1 | 13 | struct JSONRPCError <: Exception |
2 | 14 | code::Int |
3 | 15 | msg::AbstractString |
4 | 16 | data::Any |
5 | 17 | end |
6 | 18 |
|
| 19 | +""" |
| 20 | + SERVER_ERROR_END |
| 21 | +
|
| 22 | +The end of the range of server-reserved errors. |
| 23 | +
|
| 24 | +These are JSON-RPC server errors that are free for the taking |
| 25 | +for JSON-RPC server implementations. Applications making use of |
| 26 | +this library should NOT define new errors in this range. |
| 27 | +""" |
| 28 | +const SERVER_ERROR_END = -32000 |
| 29 | + |
| 30 | +""" |
| 31 | + SERVER_ERROR_START |
| 32 | +
|
| 33 | +The start of the range of server-reserved errors. |
| 34 | +
|
| 35 | +These are JSON-RPC server errors that are free for the taking |
| 36 | +for JSON-RPC server implementations. Applications making use of |
| 37 | +this library should NOT define new errors in this range. |
| 38 | +""" |
| 39 | +const SERVER_ERROR_START = -32099 |
| 40 | + |
| 41 | +""" |
| 42 | + PARSE_ERROR |
| 43 | +
|
| 44 | +Invalid JSON was received by the server. |
| 45 | +An error occurred on the server while parsing the JSON text. |
| 46 | +""" |
| 47 | +const PARSE_ERROR = -32700 |
| 48 | + |
| 49 | +""" |
| 50 | + INVALID_REQUEST |
| 51 | +
|
| 52 | +The JSON sent is not a valid Request object. |
| 53 | +""" |
| 54 | +const INVALID_REQUEST = -32600 |
| 55 | + |
| 56 | +""" |
| 57 | + METHOD_NOT_FOUND |
| 58 | +
|
| 59 | +The method does not exist / is not available. |
| 60 | +""" |
| 61 | +const METHOD_NOT_FOUND = -32601 |
| 62 | + |
| 63 | +""" |
| 64 | + INVALID_PARAMS |
| 65 | +
|
| 66 | +Invalid method parameter(s). |
| 67 | +""" |
| 68 | +const INVALID_PARAMS = -32602 |
| 69 | + |
| 70 | +""" |
| 71 | + INTERNAL_ERROR |
| 72 | +
|
| 73 | +Internal JSON-RPC error. |
| 74 | +""" |
| 75 | +const INTERNAL_ERROR = -32603 |
| 76 | + |
| 77 | +""" |
| 78 | + RPCErrorStrings |
| 79 | +
|
| 80 | +A `Base.IdDict` containing the mapping of JSON-RPC error codes to a short, descriptive string. |
| 81 | +
|
| 82 | +Use this to hook into `showerror(io::IO, ::JSONRPCError)` for display purposes. A default fallback to `"Unknown"` exists. |
| 83 | +""" |
| 84 | +const RPCErrorStrings = Base.IdDict( |
| 85 | + PARSE_ERROR => "ParseError", |
| 86 | + INVALID_REQUEST => "InvalidRequest", |
| 87 | + METHOD_NOT_FOUND => "MethodNotFound", |
| 88 | + INVALID_PARAMS => "InvalidParams", |
| 89 | + INTERNAL_ERROR => "InternalError", |
| 90 | + [ i => "ServerError" for i in SERVER_ERROR_START:SERVER_ERROR_END]..., |
| 91 | + -32002 => "ServerNotInitialized", |
| 92 | + -32001 => "UnknownErrorCode", |
| 93 | +) |
| 94 | + |
7 | 95 | function Base.showerror(io::IO, ex::JSONRPCError) |
8 | | - error_code_as_string = if ex.code == -32700 |
9 | | - "ParseError" |
10 | | - elseif ex.code == -32600 |
11 | | - "InvalidRequest" |
12 | | - elseif ex.code == -32601 |
13 | | - "MethodNotFound" |
14 | | - elseif ex.code == -32602 |
15 | | - "InvalidParams" |
16 | | - elseif ex.code == -32603 |
17 | | - "InternalError" |
18 | | - elseif ex.code == -32099 |
19 | | - "serverErrorStart" |
20 | | - elseif ex.code == -32000 |
21 | | - "serverErrorEnd" |
22 | | - elseif ex.code == -32002 |
23 | | - "ServerNotInitialized" |
24 | | - elseif ex.code == -32001 |
25 | | - "UnknownErrorCode" |
26 | | - elseif ex.code == -32800 |
27 | | - "RequestCancelled" |
28 | | - elseif ex.code == -32801 |
29 | | - "ContentModified" |
30 | | - else |
31 | | - "Unkonwn" |
32 | | - end |
| 96 | + error_code_as_string = get(RPCErrorStrings, ex.code, "Unknown") |
33 | 97 |
|
34 | 98 | print(io, error_code_as_string) |
35 | 99 | print(io, ": ") |
|
0 commit comments