# All files / src/lib error-message.ts

96.55% Statements 28/29
85.18% Branches 23/27
100% Functions 3/3
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    381x
    26x
    13x
    13x

    1x
    6x
    6x

    4x
    4x
    4x

    3x
    1x
    1x

    2x
    2x

    1x

    7x
    7x

    7x
    7x

    7x
    5x

    2x

     const getNonEmptyString = (value: unknown): string | null => {
      if (typeof value !== 'string') return null;
      const trimmed = value.trim();
      return trimmed.length > 0 ? trimmed : null;
    };

    const getMessageFromRecord = (record: Record<string, unknown>): string | null => {
      const details = getNonEmptyString(record.details);
      if (details) return details;

      const errorField = record.error;
      const errorText = getNonEmptyString(errorField);
      if (errorText) return errorText;

      if (typeof errorField === 'object' && errorField !== null) {
        const nestedError = getMessageFromRecord(errorField as Record<string, unknown>);
        Eif (nestedError) return nestedError;
      }

      const message = getNonEmptyString(record.message);
      if (message) return message;

      return null;
    };

    export function extractErrorMessage(error: unknown, fallback = 'An error occurred'): string {
      const fallbackText = getNonEmptyString(fallback) ?? 'An error occurred';

      const directErrorText = getNonEmptyString(error);
      Iif (directErrorText) return directErrorText;

      if (typeof error === 'object' && error !== null) {
        return getMessageFromRecord(error as Record<string, unknown>) ?? fallbackText;
      }

      return fallbackText;
    }