Skip to content

Commit bc46923

Browse files
committed
Allow more customization of RequestError messages
1 parent 205747a commit bc46923

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

src/acp.ts

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,50 +1065,88 @@ export class RequestError extends Error {
10651065
/**
10661066
* Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
10671067
*/
1068-
static parseError(data?: object): RequestError {
1069-
return new RequestError(-32700, "Parse error", data);
1068+
static parseError(data?: unknown, additionalMessage?: string): RequestError {
1069+
return new RequestError(
1070+
-32700,
1071+
`Parse error${additionalMessage ? `: ${additionalMessage}` : ""}`,
1072+
data,
1073+
);
10701074
}
10711075

10721076
/**
10731077
* The JSON sent is not a valid Request object.
10741078
*/
1075-
static invalidRequest(data?: object): RequestError {
1076-
return new RequestError(-32600, "Invalid request", data);
1079+
static invalidRequest(
1080+
data?: unknown,
1081+
additionalMessage?: string,
1082+
): RequestError {
1083+
return new RequestError(
1084+
-32600,
1085+
`Invalid request${additionalMessage ? `: ${additionalMessage}` : ""}`,
1086+
data,
1087+
);
10771088
}
10781089

10791090
/**
10801091
* The method does not exist / is not available.
10811092
*/
10821093
static methodNotFound(method: string): RequestError {
1083-
return new RequestError(-32601, "Method not found", { method });
1094+
return new RequestError(-32601, `"Method not found": ${method}`, {
1095+
method,
1096+
});
10841097
}
10851098

10861099
/**
10871100
* Invalid method parameter(s).
10881101
*/
1089-
static invalidParams(data?: object): RequestError {
1090-
return new RequestError(-32602, "Invalid params", data);
1102+
static invalidParams(
1103+
data?: unknown,
1104+
additionalMessage?: string,
1105+
): RequestError {
1106+
return new RequestError(
1107+
-32602,
1108+
`Invalid params${additionalMessage ? `: ${additionalMessage}` : ""}`,
1109+
data,
1110+
);
10911111
}
10921112

10931113
/**
10941114
* Internal JSON-RPC error.
10951115
*/
1096-
static internalError(data?: object): RequestError {
1097-
return new RequestError(-32603, "Internal error", data);
1116+
static internalError(
1117+
data?: unknown,
1118+
additionalMessage?: string,
1119+
): RequestError {
1120+
return new RequestError(
1121+
-32603,
1122+
`Internal error${additionalMessage ? `: ${additionalMessage}` : ""}`,
1123+
data,
1124+
);
10981125
}
10991126

11001127
/**
11011128
* Authentication required.
11021129
*/
1103-
static authRequired(data?: object): RequestError {
1104-
return new RequestError(-32000, "Authentication required", data);
1130+
static authRequired(
1131+
data?: unknown,
1132+
additionalMessage?: string,
1133+
): RequestError {
1134+
return new RequestError(
1135+
-32000,
1136+
`Authentication required${additionalMessage ? `: ${additionalMessage}` : ""}`,
1137+
data,
1138+
);
11051139
}
11061140

11071141
/**
11081142
* Resource, such as a file, was not found
11091143
*/
11101144
static resourceNotFound(uri?: string): RequestError {
1111-
return new RequestError(-32002, "Resource not found", uri && { uri });
1145+
return new RequestError(
1146+
-32002,
1147+
`Resource not found${uri ? `: ${uri}` : ""}`,
1148+
uri && { uri },
1149+
);
11121150
}
11131151

11141152
toResult<T>(): Result<T> {

0 commit comments

Comments
 (0)