Skip to content

Commit f0cc89b

Browse files
committed
Add ifError overloads for BooleanExpression
1 parent f9fef48 commit f0cc89b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

packages/firestore/src/lite-api/expressions.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,30 @@ export class BooleanExpression extends FunctionExpression {
26562656
'conditional'
26572657
);
26582658
}
2659+
2660+
/**
2661+
* @beta
2662+
*
2663+
* Creates an expression that returns the `catch` argument if there is an
2664+
* error, else return the result of this expression.
2665+
*
2666+
* ```typescript
2667+
* // Create an expression that protects against a divide by zero error
2668+
* // but always returns a boolean expression.
2669+
* constant(50).divide('length').gt(1).ifError(constant(false));
2670+
* ```
2671+
*
2672+
* @param catchValue The value that will be returned if this expression
2673+
* produces an error.
2674+
* @return A new {@code Expr} representing the 'ifError' operation.
2675+
*/
2676+
ifError(catchValue: BooleanExpression): BooleanExpression {
2677+
return new BooleanExpression(
2678+
'if_error',
2679+
[this, catchValue],
2680+
'ifError'
2681+
);
2682+
}
26592683
}
26602684

26612685
/**
@@ -2814,6 +2838,30 @@ export function isError(value: Expression): BooleanExpression {
28142838
return value.isError();
28152839
}
28162840

2841+
/**
2842+
* @beta
2843+
*
2844+
* Creates an expression that returns the `catch` argument if there is an
2845+
* error, else return the result of the `try` argument evaluation.
2846+
*
2847+
* This overload is useful when a BooleanExpression is required.
2848+
*
2849+
* ```typescript
2850+
* // Create an expression that protects against a divide by zero error
2851+
* // but always returns a boolean expression.
2852+
* ifError(constant(50).divide('length').gt(1), constant(false));
2853+
* ```
2854+
*
2855+
* @param tryExpr The try expression.
2856+
* @param catchExpr The catch expression that will be evaluated and
2857+
* returned if the tryExpr produces an error.
2858+
* @return A new {@code Expr} representing the 'ifError' operation.
2859+
*/
2860+
export function ifError(
2861+
tryExpr: BooleanExpression,
2862+
catchExpr: BooleanExpression
2863+
): BooleanExpression;
2864+
28172865
/**
28182866
* @beta
28192867
*
@@ -2861,7 +2909,11 @@ export function ifError(
28612909
tryExpr: Expression,
28622910
catchValue: unknown
28632911
): FunctionExpression {
2864-
return tryExpr.ifError(valueToDefaultExpr(catchValue));
2912+
if (tryExpr instanceof BooleanExpression && catchValue instanceof BooleanExpression) {
2913+
return tryExpr.ifError(catchValue);
2914+
} else {
2915+
return tryExpr.ifError(valueToDefaultExpr(catchValue));
2916+
}
28652917
}
28662918

28672919
/**

packages/firestore/test/integration/api/pipeline.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,9 @@ apiDescribe.only('Pipelines', persistence => {
25832583
ifError(divide(constant(1), constant(0)), constant('was error')).as(
25842584
'ifError'
25852585
),
2586+
ifError(divide(constant(1), constant(0)).greaterThan(1), constant(true)).not().as(
2587+
'ifErrorBooleanExpression'
2588+
),
25862589
isAbsent('foo').as('isAbsent'),
25872590
isNotNull('title').as('titleIsNotNull'),
25882591
isNotNan('cost').as('costIsNotNan'),
@@ -2595,6 +2598,7 @@ apiDescribe.only('Pipelines', persistence => {
25952598
ratingIsNaN: false,
25962599
isError: true,
25972600
ifError: 'was error',
2601+
ifErrorBooleanExpression: false,
25982602
isAbsent: true,
25992603
titleIsNotNull: true,
26002604
costIsNotNan: false,
@@ -2615,6 +2619,9 @@ apiDescribe.only('Pipelines', persistence => {
26152619
divide(constant(1), constant(0))
26162620
.ifError(constant('was error'))
26172621
.as('ifError'),
2622+
divide(constant(1), constant(0)).greaterThan(1).ifError(constant(true)).not().as(
2623+
'ifErrorBooleanExpression'
2624+
),
26182625
field('foo').isAbsent().as('isAbsent'),
26192626
field('title').isNotNull().as('titleIsNotNull'),
26202627
field('cost').isNotNan().as('costIsNotNan')
@@ -2625,6 +2632,7 @@ apiDescribe.only('Pipelines', persistence => {
26252632
ratingIsNaN: false,
26262633
isError: true,
26272634
ifError: 'was error',
2635+
ifErrorBooleanExpression: false,
26282636
isAbsent: true,
26292637
titleIsNotNull: true,
26302638
costIsNotNan: false

0 commit comments

Comments
 (0)