Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken authored Jan 10, 2025
1 parent e1e2944 commit 7faf268
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/vs/base/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,14 @@ export interface SerializedError {
readonly message: string;
readonly stack: string;
readonly noTelemetry: boolean;
readonly code?: string;
readonly cause?: SerializedError;
}

type ErrorWithCode = Error & {
code: string | undefined;
};

export function transformErrorForSerialization(error: Error): SerializedError;
export function transformErrorForSerialization(error: any): any;
export function transformErrorForSerialization(error: any): any {
Expand All @@ -141,7 +146,8 @@ export function transformErrorForSerialization(error: any): any {
message,
stack,
noTelemetry: ErrorNoTelemetry.isErrorNoTelemetry(error),
cause: cause ? transformErrorForSerialization(cause) : undefined
cause: cause ? transformErrorForSerialization(cause) : undefined,
code: (<ErrorWithCode>error).code
};
}

Expand All @@ -159,6 +165,9 @@ export function transformErrorFromSerialization(data: SerializedError): Error {
}
error.message = data.message;
error.stack = data.stack;
if (data.code) {
(<ErrorWithCode>error).code = data.code;
}
if (data.cause) {
error.cause = transformErrorFromSerialization(data.cause);
}
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/api/browser/mainThreadLanguageModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AuthenticationSession, AuthenticationSessionsChangeEvent, IAuthenticati
import { IExtHostContext, extHostNamedCustomer } from '../../services/extensions/common/extHostCustomers.js';
import { IExtensionService } from '../../services/extensions/common/extensions.js';
import { ExtHostContext, ExtHostLanguageModelsShape, MainContext, MainThreadLanguageModelsShape } from '../common/extHost.protocol.js';
import { LanguageModelError } from '../common/extHostTypes.js';

@extHostNamedCustomer(MainContext.MainThreadLanguageModels)
export class MainThreadLanguageModels implements MainThreadLanguageModelsShape {
Expand Down Expand Up @@ -97,7 +98,7 @@ export class MainThreadLanguageModels implements MainThreadLanguageModelsShape {
if (data) {
this._pendingProgress.delete(requestId);
if (err) {
const error = transformErrorFromSerialization(err);
const error = LanguageModelError.tryDeserialize(err) ?? transformErrorFromSerialization(err);
data.stream.reject(error);
data.defer.error(error);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHostLanguageModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export class ExtHostLanguageModels implements ExtHostLanguageModelsShape {
if (error) {
// we error the stream because that's the only way to signal
// that the request has failed
data.res.reject(transformErrorFromSerialization(error));
data.res.reject(extHostTypes.LanguageModelError.tryDeserialize(error) ?? transformErrorFromSerialization(error));
} else {
data.res.resolve();
}
Expand Down
13 changes: 11 additions & 2 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type * as vscode from 'vscode';
import { asArray, coalesceInPlace, equals } from '../../../base/common/arrays.js';
import { illegalArgument } from '../../../base/common/errors.js';
import { illegalArgument, SerializedError } from '../../../base/common/errors.js';
import { IRelativePattern } from '../../../base/common/glob.js';
import { MarkdownString as BaseMarkdownString, MarkdownStringTrustedOptions } from '../../../base/common/htmlContent.js';
import { ResourceMap } from '../../../base/common/map.js';
Expand Down Expand Up @@ -4847,6 +4847,8 @@ export class LanguageModelChatAssistantMessage {

export class LanguageModelError extends Error {

static readonly #name = 'LanguageModelError';

static NotFound(message?: string): LanguageModelError {
return new LanguageModelError(message, LanguageModelError.NotFound.name);
}
Expand All @@ -4859,11 +4861,18 @@ export class LanguageModelError extends Error {
return new LanguageModelError(message, LanguageModelError.Blocked.name);
}

static tryDeserialize(data: SerializedError): LanguageModelError | undefined {
if (data.name !== LanguageModelError.#name) {
return undefined;
}
return new LanguageModelError(data.message, data.code, data.cause);
}

readonly code: string;

constructor(message?: string, code?: string, cause?: Error) {
super(message, { cause });
this.name = 'LanguageModelError';
this.name = LanguageModelError.#name;
this.code = code ?? '';
}

Expand Down

0 comments on commit 7faf268

Please sign in to comment.