flora-exception
Version:
An exception system for FQL.
98 lines (87 loc) • 2.85 kB
text/typescript
import {
query,
ExprArg
} from "faunadb";
import {
generate
} from "shortid";
import { ContainsException, FloraException, GetExceptions } from "./Exception";
import { FloraLocalState } from "./Fx";
import { Reraise } from "./Raise";
const {
ContainsPath,
If,
Select,
Var,
Let,
Map,
} = query;
export const expressArgs = <A extends any[]>(args : A, evaluatedArgs : ExprArg, loc : string) : A=>{
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
})
)
}) as A
}
export interface BackendYieldArgsI<A extends any[], T>{
name : string,
args : A,
expr : T
}
export const BackendYield = <A extends any[], T>(args : BackendYieldArgsI<A, T>) : T=>{
return Let(
{
[bargs] : Select("args", args), // need to evaluate the args so that we do not have any stateful nonesense
[result] : If(
ContainsException(Var(bargs)),
Reraise(GetExceptions(Var(bargs) as unknown as any[]), FloraException({
name : "ReraisedException",
msg : "This exception was reraised in a yield expression.",
location : Select("name", args) as unknown as string
})),
Select("expr", args)
)
},
Var(result)
) as unknown as T
}
export const BackendYieldFunctionName = "Backend-Yield-Flora-Exception";
export const DeployBackendYield = ()=>{
}
/*export const CallBackendYield = <A extends any[], T>(args : BackendYieldArgsI<A, T>) : T=>{
}*/
export interface FrontendYieldArgsI<A extends any[], T>{
name : string,
args : A,
expr : (...args : A)=>T
}
const bargs = "bargs";
const result = "result";
/**
* Yields the result of an expression.
* @param args
* @returns
*/
export const FrontendYield = <A extends any[], T>(args : FrontendYieldArgsI<A, T>) : T=>{
return Let(
{
[bargs] : Select("args", args), // need to evaluate the args so that we do not have any stateful nonesense
[result] : If(
ContainsException(Var(bargs)),
Reraise(GetExceptions(Var(bargs) as unknown as any[]), FloraException({
name : "ReraisedException",
msg : "This exception was reraised in a yield expression.",
location : Select("name", args) as unknown as string
})),
args.expr(...expressArgs(args.args, Var(bargs), args.name))
)
},
Var(result)
) as unknown as T
}