-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move @boundaryml/baml errors into it's own file
feat: update types /client/ server to work better with streaming
- Loading branch information
Showing
26 changed files
with
213 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
engine/language_client_typescript/typescript_src/errors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
export class BamlClientFinishReasonError extends Error { | ||
prompt: string; | ||
raw_output: string; | ||
|
||
constructor(prompt: string, raw_output: string, message: string) { | ||
super(message); | ||
this.name = "BamlClientFinishReasonError"; | ||
this.prompt = prompt; | ||
this.raw_output = raw_output; | ||
|
||
Object.setPrototypeOf(this, BamlClientFinishReasonError.prototype); | ||
} | ||
|
||
toJSON(): string { | ||
return JSON.stringify( | ||
{ | ||
name: this.name, | ||
message: this.message, | ||
raw_output: this.raw_output, | ||
prompt: this.prompt, | ||
}, | ||
null, | ||
2 | ||
); | ||
} | ||
|
||
static from(error: Error): BamlClientFinishReasonError | undefined { | ||
if (error.message.includes("BamlClientFinishReasonError")) { | ||
try { | ||
const errorData = JSON.parse(error.message); | ||
if (errorData.type === "BamlClientFinishReasonError") { | ||
return new BamlClientFinishReasonError( | ||
errorData.prompt || "", | ||
errorData.raw_output || "", | ||
errorData.message || error.message | ||
); | ||
} else { | ||
console.warn("Not a BamlClientFinishReasonError:", error); | ||
} | ||
} catch (parseError) { | ||
// If JSON parsing fails, fall back to the original error | ||
console.warn("Failed to parse BamlClientFinishReasonError:", parseError); | ||
} | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
export class BamlValidationError extends Error { | ||
prompt: string; | ||
raw_output: string; | ||
|
||
constructor(prompt: string, raw_output: string, message: string) { | ||
super(message); | ||
this.name = "BamlValidationError"; | ||
this.prompt = prompt; | ||
this.raw_output = raw_output; | ||
|
||
Object.setPrototypeOf(this, BamlValidationError.prototype); | ||
} | ||
|
||
toJSON(): string { | ||
return JSON.stringify( | ||
{ | ||
name: this.name, | ||
message: this.message, | ||
raw_output: this.raw_output, | ||
prompt: this.prompt, | ||
}, | ||
null, | ||
2 | ||
); | ||
} | ||
|
||
static from(error: Error): BamlValidationError | undefined { | ||
if (error.message.includes("BamlValidationError")) { | ||
try { | ||
const errorData = JSON.parse(error.message); | ||
if (errorData.type === "BamlValidationError") { | ||
return new BamlValidationError( | ||
errorData.prompt || "", | ||
errorData.raw_output || "", | ||
errorData.message || error.message | ||
); | ||
} | ||
} catch (parseError) { | ||
console.warn("Failed to parse BamlValidationError:", parseError); | ||
} | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
// Helper function to safely create a BamlValidationError | ||
export function createBamlValidationError( | ||
error: Error | ||
): BamlValidationError | BamlClientFinishReasonError | Error { | ||
const bamlValidationError = BamlValidationError.from(error); | ||
if (bamlValidationError) { | ||
return bamlValidationError; | ||
} | ||
|
||
const bamlClientFinishReasonError = BamlClientFinishReasonError.from(error); | ||
if (bamlClientFinishReasonError) { | ||
return bamlClientFinishReasonError; | ||
} | ||
|
||
// otherwise return the original error | ||
return error; | ||
} |
Oops, something went wrong.