Skip to content

fallback

awekrx edited this page May 29, 2026 · 1 revision

fallback

Import

import { fallback } from '@dev-suite/decorators/fallback'

Category

  • method

Use Case

Return fallback value/handler output when primary call fails.

Replaces

  • Method-level try/catch with default return
  • Repeated error fallback blocks

Example 1

Without decorator

class FeedService {
  async list() {
    try {
      return await this.remote.list();
    } catch {
      return [];
    }
  }
}

With decorator

import { fallback } from '@dev-suite/decorators/fallback';

class FeedService {
  @fallback({ fallbackValue: [] })
  async list() {
    return this.remote.list();
  }
}

Why better

  • Fallback intent is explicit and reusable.
  • Primary path is uncluttered by recovery boilerplate.

Example 2

Without decorator

class ProfileService {
  async getAvatar(userId: string) {
    try {
      return await this.cdn.avatar(userId);
    } catch (error) {
      this.logger.warn('avatar fallback', { userId, error });
      return this.assets.defaultAvatar;
    }
  }
}

With decorator

import { fallback } from '@dev-suite/decorators/fallback';

class ProfileService {
  @fallback({ onError: () => this.assets.defaultAvatar })
  async getAvatar(userId: string) {
    return this.cdn.avatar(userId);
  }
}

Why better

  • Custom recovery logic moves to config instead of method body.
  • Easier to enforce consistent fallback behavior.

Clone this wiki locally