< Summary - Envilder Node.js SDK

Information
Class: src/sdks/nodejs/src/infrastructure/describe-error.ts
Assembly: Default
File(s): src/sdks/nodejs/src/infrastructure/describe-error.ts
Tag: 605_44106794472
Line coverage
50%
Covered lines: 6
Uncovered lines: 6
Coverable lines: 12
Total lines: 28
Line coverage: 50%
Branch coverage
31%
Covered branches: 5
Total branches: 16
Branch coverage: 31.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/sdks/nodejs/src/infrastructure/describe-error.ts

#LineLine coverage
 1/**
 2 * Extracts a human-readable reason from an unknown thrown value.
 3 *
 4 * Digs through `AggregateError.errors` and `Error.cause` so the reason is never
 5 * empty: the AWS SDK v3 wraps connection failures in an `AggregateError` whose
 6 * own `message` is empty and whose real causes live in `errors`.
 7 */
 8export function describeError(error: unknown): string {
 99  const reasons = [...new Set(collectReasons(error))];
 910  return reasons.length > 0 ? reasons.join('; ') : 'unknown error';
 11}
 12
 13function collectReasons(error: unknown): string[] {
 914  if (error instanceof AggregateError && error.errors.length > 0) {
 015    return error.errors.flatMap(collectReasons);
 16  }
 917  if (error instanceof Error) {
 918    if (error.message) {
 919      return [error.message];
 20    }
 021    if (error.cause !== undefined) {
 022      return collectReasons(error.cause);
 23    }
 024    return error.name ? [error.name] : [];
 25  }
 026  const text = String(error);
 027  return text ? [text] : [];
 28}

Methods/Properties

describeError
collectReasons