Preserve error code in the error message from RPC (#8191)

Needed for #8158
This commit is contained in:
Ilya Bogdanov 2023-10-31 19:01:54 +04:00 committed by GitHub
parent f2cfd7f86c
commit 660f4b35ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ import { Client } from '@open-rpc/client-js'
import { ObservableV2 } from 'lib0/observable' import { ObservableV2 } from 'lib0/observable'
import { uuidv4 } from 'lib0/random' import { uuidv4 } from 'lib0/random'
import { SHA3 } from 'sha3' import { SHA3 } from 'sha3'
import { z } from 'zod'
import type { import type {
Checksum, Checksum,
ContextId, ContextId,
@ -22,11 +23,76 @@ import type { Uuid } from './yjsModel'
const DEBUG_LOG_RPC = false const DEBUG_LOG_RPC = false
const RPC_TIMEOUT_MS = 15000 const RPC_TIMEOUT_MS = 15000
export enum ErrorCode {
ACCESS_DENIED = 100,
FILE_SYSTEM_ERROR = 1000,
CONTENT_ROOT_NOT_FOUND = 1001,
FILE_NOT_FOUND = 1003,
FILE_EXISTS = 1004,
OPERATION_TIMEOUT = 1005,
NOT_DIRECTORY = 1006,
NOT_FILE = 1007,
CANNOT_OVERWRITE = 1008,
READ_OUT_OF_BOUNDS = 1009,
CANNOT_DECODE = 1010,
STACK_ITEM_NOT_FOUND = 2001,
CONTEXT_NOT_FOUND = 2002,
EMPTY_STACK = 2003,
INVALID_STACK_ITEM = 2004,
MODULE_NOT_FOUND = 2005,
VISUALIZATION_NOT_FOUND = 2006,
VISUALIZATION_EXPRESSION_ERROR = 2007,
FILE_NOT_OPENED = 3001,
TEXT_EDIT_VALIDATION_ERROR = 3002,
INVALID_VERSION = 3003,
WRITE_DENIED = 3004,
CAPABILITY_NOT_ACQUIRED = 5001,
SESSION_NOT_INITIALIZED = 6001,
SESSION_ALREADY_INITIALIZED = 6002,
RESOURCES_INITIALIZATION_ERROR = 6003,
SUGGESTION_DATABASE_ERROR = 7001,
PROJECT_NOT_FOUND = 7002,
MODULE_NAME_NOT_RESOLVED = 7003,
SUGGESTION_NOT_FOUND = 7004,
EDITION_NOT_FOUND = 8001,
LIBRARY_ALREADY_EXISTS = 8002,
LIBRARY_REPOSITORY_AUTHENTICATION_ERROR = 8003,
LIBRARY_PUBLISH_ERROR = 8004,
LIBRARY_UPLOAD_ERROR = 8005,
LIBRARY_DOWNLOAD_ERROR = 8006,
LOCAL_LIBRARY_NOT_FOUND = 8007,
LIBRARY_NOT_RESOLVED = 8008,
INVALID_LIBRARY_NAME = 8009,
DEPENDENCY_DISCOVERY_ERROR = 8010,
INVALID_SEMVER_VERSION = 8011,
EXPRESSION_NOT_FOUND = 9001,
FAILED_TO_APPLY_EDITS = 9002,
REFACTORING_NOT_SUPPORTED = 9003,
}
const RemoteRpcErrorSchema = z.object({
code: z.nativeEnum(ErrorCode),
message: z.string(),
data: z.optional(z.any()),
})
type RemoteRpcErrorParsed = z.infer<typeof RemoteRpcErrorSchema>
export class RemoteRpcError {
code: ErrorCode
message: string
data?: any
constructor(error: RemoteRpcErrorParsed) {
this.code = error.code
this.message = error.message
this.data = error.data
}
}
export class LsRpcError extends Error { export class LsRpcError extends Error {
cause: Error cause: RemoteRpcError | Error
request: string request: string
params: object params: object
constructor(cause: Error, request: string, params: object) { constructor(cause: RemoteRpcError | Error, request: string, params: object) {
super(`Language server request '${request}' failed.`) super(`Language server request '${request}' failed.`)
this.cause = cause this.cause = cause
this.request = request this.request = request
@ -64,7 +130,10 @@ export class LanguageServer extends ObservableV2<Notifications> {
} }
return await this.client.request({ method, params }, RPC_TIMEOUT_MS) return await this.client.request({ method, params }, RPC_TIMEOUT_MS)
} catch (e) { } catch (e) {
if (e instanceof Error) { const remoteError = RemoteRpcErrorSchema.safeParse(e)
if (remoteError.success) {
throw new LsRpcError(new RemoteRpcError(remoteError.data), method, params)
} else if (e instanceof Error) {
throw new LsRpcError(e, method, params) throw new LsRpcError(e, method, params)
} }
throw e throw e