Skip to content

Commit 18f5b9a

Browse files
authored
jsonrpc: add Other constructor for error codes (#1045)
* jsonrpc: add Other constructor for error codes * jsonrpc: make Error.Code.of_int non-optional
1 parent e2c3e69 commit 18f5b9a

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

jsonrpc/src/jsonrpc.ml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,24 @@ module Response = struct
114114
| ServerCancelled
115115
| ContentModified
116116
| RequestCancelled
117+
(* all other codes are custom *)
118+
| Other of int
117119

118120
let of_int = function
119-
| -32700 -> Some ParseError
120-
| -32600 -> Some InvalidRequest
121-
| -32601 -> Some MethodNotFound
122-
| -32602 -> Some InvalidParams
123-
| -32603 -> Some InternalError
124-
| -32099 -> Some ServerErrorStart
125-
| -32000 -> Some ServerErrorEnd
126-
| -32002 -> Some ServerNotInitialized
127-
| -32001 -> Some UnknownErrorCode
128-
| -32800 -> Some RequestCancelled
129-
| -32801 -> Some ContentModified
130-
| -32802 -> Some ServerCancelled
131-
| -32803 -> Some RequestFailed
132-
| _ -> None
121+
| -32700 -> ParseError
122+
| -32600 -> InvalidRequest
123+
| -32601 -> MethodNotFound
124+
| -32602 -> InvalidParams
125+
| -32603 -> InternalError
126+
| -32099 -> ServerErrorStart
127+
| -32000 -> ServerErrorEnd
128+
| -32002 -> ServerNotInitialized
129+
| -32001 -> UnknownErrorCode
130+
| -32800 -> RequestCancelled
131+
| -32801 -> ContentModified
132+
| -32802 -> ServerCancelled
133+
| -32803 -> RequestFailed
134+
| code -> Other code
133135

134136
let to_int = function
135137
| ParseError -> -32700
@@ -145,13 +147,11 @@ module Response = struct
145147
| ContentModified -> -32801
146148
| ServerCancelled -> -32802
147149
| RequestFailed -> -32803
150+
| Other code -> code
148151

149152
let t_of_yojson json =
150153
match json with
151-
| `Int i -> (
152-
match of_int i with
153-
| None -> Json.error "unknown code" json
154-
| Some i -> i)
154+
| `Int i -> of_int i
155155
| _ -> Json.error "invalid code" json
156156

157157
let yojson_of_t t = `Int (to_int t)

jsonrpc/src/jsonrpc.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module Response : sig
9393
| ServerCancelled
9494
| ContentModified
9595
| RequestCancelled
96+
| Other of int
9697
end
9798

9899
type t =

0 commit comments

Comments
 (0)