flora-exception
Version:
An exception system for FQL.
44 lines (43 loc) • 1.67 kB
JavaScript
import { query } from "faunadb";
import { ContainsException, FloraException, GetExceptions } from "./Exception";
import { Reraise } from "./Raise";
const { ContainsPath, If, Select, Var, Let, Map, } = query;
export const expressArgs = (args, evaluatedArgs, loc) => {
return args.map((arg, index) => {
return If(ContainsPath(index, evaluatedArgs), Select(index, evaluatedArgs), FloraException({
name: "UndefinedArgException",
msg: `The arg at index ${index} was not defined.`,
location: loc
}));
});
};
export const BackendYield = (args) => {
return Let({
[bargs]: Select("args", args),
[result]: If(ContainsException(Var(bargs)), Reraise(GetExceptions(Var(bargs)), FloraException({
name: "ReraisedException",
msg: "This exception was reraised in a yield expression.",
location: Select("name", args)
})), Select("expr", args))
}, Var(result));
};
export const BackendYieldFunctionName = "Backend-Yield-Flora-Exception";
export const DeployBackendYield = () => {
};
const bargs = "bargs";
const result = "result";
/**
* Yields the result of an expression.
* @param args
* @returns
*/
export const FrontendYield = (args) => {
return Let({
[bargs]: Select("args", args),
[result]: If(ContainsException(Var(bargs)), Reraise(GetExceptions(Var(bargs)), FloraException({
name: "ReraisedException",
msg: "This exception was reraised in a yield expression.",
location: Select("name", args)
})), args.expr(...expressArgs(args.args, Var(bargs), args.name)))
}, Var(result));
};