lspeasy / core/src / ResponseError
Class: ResponseError
Defined in: packages/core/src/utils/errors.ts:96
An Error subclass that maps to a JSON-RPC 2.0 error response.
Remarks
Throw a ResponseError from any request handler to send a structured error response to the client. The framework catches it and converts it to the wire format automatically.
Use the static factory methods (ResponseError.invalidParams(), etc.) for the standard JSON-RPC / LSP error codes rather than constructing raw codes.
Use When
A request handler needs to reject with a machine-readable error code that the client can act on (e.g. respond with MethodNotFound when a capability was not declared, or InvalidParams when schema validation fails).
Avoid When
You want to log a server-side error without sending an error to the client — throw a plain Error and handle it via server.onError() instead.
Never
NEVER throw ResponseError with a code outside the defined ranges without documenting it. Undocumented codes are opaque to clients and tools.
Example
import { ResponseError, JSONRPCErrorCode } from '@lspeasy/core';
import { LSPServer } from '@lspeasy/server';
const server = new LSPServer();
server.onRequest('textDocument/hover', async (params) => {
const doc = getDocument(params.textDocument.uri);
if (!doc) {
throw ResponseError.invalidParams(`Unknown document: ${params.textDocument.uri}`);
}
return computeHover(doc, params.position);
});Extends
Error
Constructors
Constructor
new ResponseError(
code,
message,
data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:97
Parameters
| Parameter | Type |
|---|---|
code | number |
message | string |
data? | unknown |
Returns
ResponseError
Overrides
Error.constructorProperties
| Property | Modifier | Type | Description | Inherited from | Defined in |
|---|---|---|---|---|---|
cause? | public | unknown | - | Error.cause | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26 |
code | readonly | number | - | - | packages/core/src/utils/errors.ts:98 |
data? | readonly | unknown | - | - | packages/core/src/utils/errors.ts:100 |
message | public | string | - | Error.message | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1077 |
name | public | string | - | Error.name | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1076 |
stack? | public | string | - | Error.stack | node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1078 |
stackTraceLimit | static | number | The Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | Error.stackTraceLimit | node_modules/.pnpm/@types+node@25.6.0/node_modules/@types/node/globals.d.ts:67 |
Methods
toJSON()
toJSON(): {
code: number;
data: unknown;
message: string;
};Defined in: packages/core/src/utils/errors.ts:111
Serializes to the JSON-RPC wire format ({ code, message, data? }).
Returns
{
code: number;
data: unknown;
message: string;
}A plain object suitable for embedding in an ErrorResponseMessage.
| Name | Type | Defined in |
|---|---|---|
code | number | packages/core/src/utils/errors.ts:113 |
data | unknown | packages/core/src/utils/errors.ts:115 |
message | string | packages/core/src/utils/errors.ts:114 |
captureStackTrace()
static captureStackTrace(targetObject, constructorOpt?): void;Defined in: node_modules/.pnpm/@types+node@25.6.0/node_modules/@types/node/globals.d.ts:51
Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.
The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.
The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();Parameters
| Parameter | Type |
|---|---|
targetObject | object |
constructorOpt? | Function |
Returns
void
Inherited from
Error.captureStackTraceinternalError()
static internalError(message?, data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:182
Create an internal error.
Parameters
| Parameter | Type | Description |
|---|---|---|
message? | string | Optional override for the default error message. |
data? | unknown | Optional machine-readable error details. |
Returns
ResponseError
A ResponseError with code JSONRPCErrorCode.InternalError.
invalidParams()
static invalidParams(message?, data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:167
Create an invalid params error.
Parameters
| Parameter | Type | Description |
|---|---|---|
message? | string | Optional override for the default error message. |
data? | unknown | Optional machine-readable error details. |
Returns
ResponseError
A ResponseError with code JSONRPCErrorCode.InvalidParams.
invalidRequest()
static invalidRequest(message?, data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:141
Create an invalid request error.
Parameters
| Parameter | Type | Description |
|---|---|---|
message? | string | Optional override for the default error message. |
data? | unknown | Optional machine-readable error details. |
Returns
ResponseError
A ResponseError with code JSONRPCErrorCode.InvalidRequest.
methodNotFound()
static methodNotFound(method, data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:156
Create a method not found error.
Parameters
| Parameter | Type | Description |
|---|---|---|
method | string | The method name that was not found. |
data? | unknown | Optional machine-readable error details. |
Returns
ResponseError
A ResponseError with code JSONRPCErrorCode.MethodNotFound.
parseError()
static parseError(message?, data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:126
Create a parse error.
Parameters
| Parameter | Type | Description |
|---|---|---|
message? | string | Optional override for the default error message. |
data? | unknown | Optional machine-readable error details. |
Returns
ResponseError
A ResponseError with code JSONRPCErrorCode.ParseError.
prepareStackTrace()
static prepareStackTrace(err, stackTraces): any;Defined in: node_modules/.pnpm/@types+node@25.6.0/node_modules/@types/node/globals.d.ts:55
Parameters
| Parameter | Type |
|---|---|
err | Error |
stackTraces | CallSite[] |
Returns
any
See
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Inherited from
Error.prepareStackTracerequestCancelled()
static requestCancelled(data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:210
Create a request cancelled error.
Parameters
| Parameter | Type | Description |
|---|---|---|
data? | unknown | Optional machine-readable error details. |
Returns
ResponseError
A ResponseError with code JSONRPCErrorCode.RequestCancelled.
serverNotInitialized()
static serverNotInitialized(data?): ResponseError;Defined in: packages/core/src/utils/errors.ts:196
Create a server not initialized error.
Parameters
| Parameter | Type | Description |
|---|---|---|
data? | unknown | Optional machine-readable error details. |
Returns
ResponseError
A ResponseError with code JSONRPCErrorCode.ServerNotInitialized.