Skip to content

Model JSON-RPC/LSP error codes as an enum instead of magic ints #368

Description

@Firehed

Summary

ResponseError (src/Protocol/ResponseError.php) hardcodes each JSON-RPC / LSP
error code as a magic integer inside a static factory (parseError -> -32700,
invalidRequest -> -32600, serverNotInitialized -> -32002, etc.). These codes
are a fixed, spec-defined set (JSON-RPC 2.0 + LSP 3.17), which is exactly what an
int-backed enum models: it names the numbers in one place and removes the inline
literals.

Proposed change

Introduce something like:

enum ErrorCode: int
{
    case ParseError = -32700;
    case InvalidRequest = -32600;
    case MethodNotFound = -32601;
    case InvalidParams = -32602;
    case InternalError = -32603;
    case ServerNotInitialized = -32002;
}

and have ResponseError carry it (adjusting jsonSerialize() to emit
$this->code->value).

Design decision to make

JSON-RPC reserves -32000..-32099 for server-defined codes and LSP permits custom
ones, so a pure enum cannot represent an arbitrary future code. Every code the
server emits today is standard, so an enum is sufficient now, but decide whether
ResponseError::$code becomes strictly the enum or ErrorCode|int before
committing to the shape.

Scope note

This is a refactor of a pre-existing pattern, deferred out of slice S1.4 (which
only added serverNotInitialized consistent with the existing factories) to keep
that slice's diff focused.


This issue was written by AI (reviewed by a human).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions