Skip to content

Commit

Permalink
Support onError for sync formulas (#3098)
Browse files Browse the repository at this point in the history
* Support onError for sync formulas

* Rebuild
  • Loading branch information
oleg-codaio authored Nov 7, 2024
1 parent e35a065 commit 2d29301
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 35 deletions.
42 changes: 27 additions & 15 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,8 @@ export interface SyncFormulaDef<
ParamDefsT extends ParamDefs,
SchemaT extends ObjectSchemaDefinition<K, L>,
ContextT extends SyncExecutionContext<any, any>,
> extends CommonPackFormulaDef<ParamDefsT> {
> extends CommonPackFormulaDef<ParamDefsT>,
OnErrorFormulaOptions {
/**
* The JavaScript function that implements this sync.
*
Expand Down Expand Up @@ -1519,11 +1520,7 @@ export function makeFormula<ParamDefsT extends ParamDefs, ResultT extends ValueT
>;
}

/**
* Base type for formula definitions accepted by {@link makeFormula}.
*/
export interface BaseFormulaDef<ParamDefsT extends ParamDefs, ResultT extends string | number | boolean | object>
extends PackFormulaDef<ParamDefsT, ResultT> {
export interface OnErrorFormulaOptions {
/**
* If specified, will catch errors in the {@link execute} function and call this
* function with the error, instead of letting them throw and the formula failing.
Expand All @@ -1534,6 +1531,13 @@ export interface BaseFormulaDef<ParamDefsT extends ParamDefs, ResultT extends st
onError?(error: Error): any;
}

/**
* Base type for formula definitions accepted by {@link makeFormula}.
*/
export interface BaseFormulaDef<ParamDefsT extends ParamDefs, ResultT extends string | number | boolean | object>
extends PackFormulaDef<ParamDefsT, ResultT>,
OnErrorFormulaOptions {}

/**
* A definition accepted by {@link makeFormula} for a formula that returns a string.
*/
Expand Down Expand Up @@ -2320,6 +2324,7 @@ export function makeSyncTable<
execute: wrappedExecute,
executeUpdate: wrappedExecuteUpdate,
executeGetPermissions,
onError,
...definition
} = maybeRewriteConnectionForFormula(formula, connectionRequirement);

Expand Down Expand Up @@ -2388,7 +2393,7 @@ export function makeSyncTable<
}

const normalizedSchema = normalizeSchema(schema);
const formulaSchema: ArraySchema<Schema> | undefined = getSchema
const formulaSchema: ArraySchema<SchemaT> | undefined = getSchema
? undefined
: {type: ValueType.Array, items: normalizedSchema};
const {identity, id, primary} = objectSchemaHelper(schema);
Expand All @@ -2408,8 +2413,16 @@ export function makeSyncTable<
params: ParamValues<ParamDefsT>,
context: ContextT,
): Promise<SyncFormulaResult<K, L, SchemaT, ContextT>> {
const syncResult = (await wrappedExecute(params, context)) || {};
const appliedSchema = context.sync.schema;
let syncResult: SyncFormulaResult<K, L, SchemaDefT>;
try {
syncResult = (await wrappedExecute(params, context)) || {};
} catch (err: any) {
// onError should throw, but if it doesn't we'll just rethrow the original error.
onError?.(err);
throw err;
}

const appliedSchema = context.sync.schema as ArraySchema<SchemaT> | undefined;
const result = responseHandler({body: syncResult.result || [], status: 200, headers: {}}, appliedSchema) as Array<
ObjectSchemaDefinitionType<K, L, SchemaT>
>;
Expand Down Expand Up @@ -2438,7 +2451,7 @@ export function makeSyncTable<
context: SyncExecutionContext,
) {
const {result} = (await wrappedExecuteUpdate(params, updates, context)) || {};
const appliedSchema = context.sync.schema;
const appliedSchema = context.sync.schema as ArraySchema<SchemaT> | undefined;
return {
result: responseHandler({body: result || [], status: 200, headers: {}}, appliedSchema),
} as SyncUpdateResult<K, L, SchemaT>;
Expand All @@ -2454,13 +2467,13 @@ export function makeSyncTable<
...definition,
cacheTtlSecs: 0,
execute,
executeUpdate: executeUpdate as any,
executeUpdate,
schema: formulaSchema,
isSyncFormula: true,
supportsUpdates: Boolean(executeUpdate),
supportsGetPermissions: Boolean(executeGetPermissions),
connectionRequirement: definition.connectionRequirement || connectionRequirement,
resultType: Type.object as any,
resultType: Type.object as TypeOf<SchemaType<SchemaT>>,
executeGetPermissions: executeGetPermissions as any,
},
getSchema: maybeRewriteConnectionForFormula(getSchema, connectionRequirement),
Expand Down Expand Up @@ -2641,9 +2654,8 @@ export function makeTranslateObjectFormula<ParamDefsT extends ParamDefs, ResultT
return context.fetcher
.fetch(requestHandler(params))
.catch(err => {
if (onError) {
return onError(err);
}
// onError should throw, but if it doesn't we'll just rethrow the original error.
onError?.(err);
throw err;
})
.then(responseHandler);
Expand Down
12 changes: 7 additions & 5 deletions dist/api.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions dist/api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions dist/bundle.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/bundle.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/reference/sdk/interfaces/core.BaseFormulaDef.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions docs/reference/sdk/interfaces/core.SyncFormulaDef.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
2 changes: 1 addition & 1 deletion testing/upload_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@ ${endpointKey ? 'endpointKey is set' : `requiresEndpointUrl is ${requiresEndpoin
const syncFormulaSchema = zodCompleteObject<
Omit<
SyncFormula<any, any, ParamDefs, ObjectSchema<any, any>, SyncExecutionContext>,
'execute' | 'executeUpdate' | 'executeGetPermissions'
'execute' | 'executeUpdate' | 'executeGetPermissions' | 'onError'
>
>({
schema: arrayPropertySchema.optional(),
Expand Down

0 comments on commit 2d29301

Please sign in to comment.