Pipes Reference: Utility Pipes
Pipes used for error handling, fallback logic, and safe deep access
This section defines Pipes used for error handling, fallback logic, and safe deep access. These include:
onError
- Usage Syntax:
{{ inputExpression | onError: fallbackValue }} - Evaluates to: any
- Description: The onError pipe acts as a catch mechanism: if an error occurs anywhere before this pipe in the expression chain, it replaces the failed result with the provided fallback value.
- If an error occurs, returns the fallbackValue.
- If no error occurs, passes through the input value normally.
- Useful for:
- Building safe, fault-tolerant pipelines without throwing user-facing errors.
- Parameters:
- value:
- Type: any
- Required: Yes
- Description: The value to substitute if an error occurs.
- Returns:
- any: The input value if successful; otherwise, the provided fallback value.
- Example 1: Safe fallback for a failing pipe
{{ 'not-a-json' | parseJSON | onError: 'Error Message' }}- Output: 'Error Message'
- Example 2: No error, normal passthrough
{{ 'Hello' | toString | onError: 'Fallback' }}- Output: 'Hello'
- Notes:
- Only catches runtime errors or parsing failures prior to reaching onError.
- Commonly paired with risky operations like parseJSON, deep mapPath, or dynamic field access.
lookup
- Usage Syntax:
{{ inputObject | lookup: 'path': 'to': 'property' }} - Evaluates to: any
- Description: The lookup pipe navigates through an object and retrieves the value located at the specified path.
- If the path does not exist, it returns undefined or can be followed by onError to handle missing values.
- Useful for:
- Safely accessing deeply nested properties without chaining multiple checks.
- Parameters:
- ...path:
- Type: string[]
- Required: Yes
- Description: A sequence of keys representing the access path.
- Returns:
- any: The value found at the specified path within the object. If the path does not exist, returns undefined.
- Example 1: Simple object lookup
{{ user | lookup: 'name' }}- Output: 'Alice' (if user = { name: 'Alice' })
- Example 2: Nested object lookup
{{ settings | lookup: 'theme': 'color' }}- Output: 'blue' (if settings = { theme: { color: 'blue' } })
- Notes:
- Each argument in the path corresponds to a nested key in the object.
- If any key along the path is missing, the lookup short-circuits to undefined.
defaultIfNull
- Usage Syntax:
{{ inputValue | defaultIfNull: fallbackValue }} - Evaluates to: any
- Description: The defaultIfNull pipe checks if the input value is null or undefined and, if so, replaces it with the specified fallback value.
- Only null and undefined trigger the replacement — other falsy values like 0 or '' are left as-is.
- Useful for:
- Providing defaults without overwriting valid falsy inputs like 0 or an empty string.
- Parameters:
- value:
- Type: any
- Required: Yes
- Description: The fallback value if the input is null or undefined.
- Returns:
- any: The original input if non-nullish; otherwise, the provided fallback value.
- Example 1: Input is null
{{ name | defaultIfNull: 'Unknown' }}- Output: 'Unknown' (if name = null)
- Example 2: Input is an empty string (not replaced)
{{ description | defaultIfNull: 'No description available' }}- Output: '' (if description = '')
- Notes:
- Only null and undefined are considered "nullish" for replacement.
- Falsy values like 0, false, and '' are kept intact.
defaultIfFalsy
- Usage Syntax:
{{ inputValue | defaultIfFalsy: fallbackValue }} - Evaluates to: any
- Description: The defaultIfFalsy pipe checks if the input is falsy according to JavaScript truthiness rules, and replaces it with the specified fallback value if so.
- Falsy values include: false, 0, '' (empty string), null, undefined, and NaN.
- If the input is truthy, it passes through unchanged.
- Useful for:
- Providing guaranteed values when input might be missing, empty, or invalid.
- Parameters:
- value:
- Type: any
- Required: Yes
- Description: The fallback value if the input is falsy.
- Returns:
- any: The input if it is truthy; otherwise, the provided fallback value.
- Example 1: Input is 0 (falsy)
{{ quantity | defaultIfFalsy: 1 }}- Output: 1 (if quantity = 0)
- Example 2: Input is truthy
{{ username | defaultIfFalsy: 'Anonymous' }}- Output: 'Alice' (if username = 'Alice')
- Notes:
- If distinguishing between null/undefined and other falsy values is important, prefer defaultIfNull instead.
Did this answer your question?
