Skip to content

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

ts
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

ts
new ResponseError(
   code, 
   message, 
   data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:97

Parameters

ParameterType
codenumber
messagestring
data?unknown

Returns

ResponseError

Overrides

ts
Error.constructor

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
cause?publicunknown-Error.causenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:26
codereadonlynumber--packages/core/src/utils/errors.ts:98
data?readonlyunknown--packages/core/src/utils/errors.ts:100
messagepublicstring-Error.messagenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-Error.namenode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstring-Error.stacknode_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe 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.stackTraceLimitnode_modules/.pnpm/@types+node@25.6.0/node_modules/@types/node/globals.d.ts:67

Methods

toJSON()

ts
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

ts
{
  code: number;
  data: unknown;
  message: string;
}

A plain object suitable for embedding in an ErrorResponseMessage.

NameTypeDefined in
codenumberpackages/core/src/utils/errors.ts:113
dataunknownpackages/core/src/utils/errors.ts:115
messagestringpackages/core/src/utils/errors.ts:114

captureStackTrace()

ts
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.

js
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:

js
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

ParameterType
targetObjectobject
constructorOpt?Function

Returns

void

Inherited from

ts
Error.captureStackTrace

internalError()

ts
static internalError(message?, data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:182

Create an internal error.

Parameters

ParameterTypeDescription
message?stringOptional override for the default error message.
data?unknownOptional machine-readable error details.

Returns

ResponseError

A ResponseError with code JSONRPCErrorCode.InternalError.


invalidParams()

ts
static invalidParams(message?, data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:167

Create an invalid params error.

Parameters

ParameterTypeDescription
message?stringOptional override for the default error message.
data?unknownOptional machine-readable error details.

Returns

ResponseError

A ResponseError with code JSONRPCErrorCode.InvalidParams.


invalidRequest()

ts
static invalidRequest(message?, data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:141

Create an invalid request error.

Parameters

ParameterTypeDescription
message?stringOptional override for the default error message.
data?unknownOptional machine-readable error details.

Returns

ResponseError

A ResponseError with code JSONRPCErrorCode.InvalidRequest.


methodNotFound()

ts
static methodNotFound(method, data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:156

Create a method not found error.

Parameters

ParameterTypeDescription
methodstringThe method name that was not found.
data?unknownOptional machine-readable error details.

Returns

ResponseError

A ResponseError with code JSONRPCErrorCode.MethodNotFound.


parseError()

ts
static parseError(message?, data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:126

Create a parse error.

Parameters

ParameterTypeDescription
message?stringOptional override for the default error message.
data?unknownOptional machine-readable error details.

Returns

ResponseError

A ResponseError with code JSONRPCErrorCode.ParseError.


prepareStackTrace()

ts
static prepareStackTrace(err, stackTraces): any;

Defined in: node_modules/.pnpm/@types+node@25.6.0/node_modules/@types/node/globals.d.ts:55

Parameters

ParameterType
errError
stackTracesCallSite[]

Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from

ts
Error.prepareStackTrace

requestCancelled()

ts
static requestCancelled(data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:210

Create a request cancelled error.

Parameters

ParameterTypeDescription
data?unknownOptional machine-readable error details.

Returns

ResponseError

A ResponseError with code JSONRPCErrorCode.RequestCancelled.


serverNotInitialized()

ts
static serverNotInitialized(data?): ResponseError;

Defined in: packages/core/src/utils/errors.ts:196

Create a server not initialized error.

Parameters

ParameterTypeDescription
data?unknownOptional machine-readable error details.

Returns

ResponseError

A ResponseError with code JSONRPCErrorCode.ServerNotInitialized.

Released under the MIT License.