Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common.throw() Function #2941

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-fireants-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quri/squiggle-lang": patch
---

Added Common.throw
11 changes: 11 additions & 0 deletions packages/squiggle-lang/src/errors/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,14 @@ export class REAmbiguous extends ErrorMessage {
return `Ambiguous Error: ${this.message}`;
}
}

// Used for user-created throw() function calls
export class REThrow extends ErrorMessage {
constructor(public msg: string) {
super(msg);
}

toString() {
return `${this.message}`;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { REThrow } from "../errors/messages.js";
import { makeDefinition } from "../library/registry/fnDefinition.js";
import {
frAny,
Expand Down Expand Up @@ -55,4 +56,22 @@ export const library = [
),
],
}),
maker.make({
name: "throw",
description:
"Throws a fatal error. There is no way in the language to catch this error.",
definitions: [
makeDefinition(
[frOptional(frNamed("message", frString))],
frAny(),
([value]) => {
if (value) {
throw new REThrow(value);
} else {
throw new REThrow("Common.throw() was called");
}
}
),
],
}),
];
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/library/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { library as booleanLibrary } from "../../fr/boolean.js";
import { library as builtinLibrary } from "../../fr/builtin.js";
import { library as commonLibrary } from "../../fr/common.js";
import { library as calculatorLibrary } from "../../fr/calculator.js";
import { library as dangerLibrary } from "../../fr/danger.js";
import { library as dateLibrary } from "../../fr/date.js";
Expand Down Expand Up @@ -54,7 +54,7 @@ const fnList: FRFunction[] = [
...unitsLibrary,
...calculatorLibrary,
...inputLibrary,
...builtinLibrary, // should go last, because has some catch-all functions
...commonLibrary, // should go last, because has some catch-all functions
];

export const registry = Registry.make(fnList);
Expand Down