All files / src error.js

86.17% Statements 81/94
87.5% Branches 7/8
100% Functions 2/2
86.17% Lines 81/94

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 1381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1305x 1305x 1305x 1305x 774x 1305x 531x 531x 1305x 1305x 1305x 1305x 1305x 1305x 1305x 1305x                           1305x 1x 1x  
/*!
 * Copyright (c) 2026 The Triauth Authors (https://www.triauth.org/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
// The issue tracker to which internal errors should be reported
const ISSUES_URL = 'https://github.com/triauth/triauth-js/issues';
 
/**
 * The base error type used throughout triauth-js, carrying the `code`, `reason`, and `message` of the
 * error object that Triauth API methods return, and `data` for the log entry.
 *
 * @class
 * @memberof Triauth
 */
export class TriauthError extends Error {
  /**
   * @param error {{code: string, reason?: string, message: string}} - The error object returned by Triauth API methods: the error
   *                                         code (e.g., `request_denied`), the reason (only on errors about one field, e.g., `too_long`),
   *                                         and a human-readable message.
   * @param [data={}] {{cause?: *, [k: string]: *}} - Optional metadata; `cause` (if present) is forwarded to the `Error` constructor and stripped from `this.data`.
   */
  constructor({code, reason, message}, data = {}) {
    super(message, {cause: data.cause});
 
    this.name = this.constructor.name;
 
    this.code = code;
    this.reason = reason;
    this.message = message;
 
    this.data = Object.assign({}, data);
    delete this.data.cause;
 
    this.ts = new Date();
  }
 
  /**
   * Normalizes any thrown value into the `{error: {code, reason?, message}}` shape returned by Triauth API methods,
   * and logs it via `config.logger`. Validation errors (the errors about one field, which carry a `reason`) are
   * logged at `info`; other `TriauthError`s at `warn`. Anything else is an internal error (`internal_error`) logged
   * at `error` with the original error as `cause`.
   *
   * @param err {*} - The thrown value (typically a `TriauthError`, but anything is accepted).
   * @param config {object} - Effective per-call config; only `config.logger` is consulted here.
   *
   * @returns {{error: {code: string, reason?: string, message: string}}}
   */
  static process(err, config) {
    if (err instanceof TriauthError) {
 
      // Validation errors are logged with info severity
      if (err.reason !== undefined) {
        config.logger?.info?.(`Validation error - ${err.message} (${err.code})`, {cause:err});
      } else {
        config.logger?.warn?.(`${err.message} (${err.code})`, {cause:err});
      }
 
      return {
        error: (err.reason === undefined)
          ? {code: err.code, message: err.message}
          : {code: err.code, reason: err.reason, message: err.message}
      };
 
    } else {
      config.logger?.error?.(
        `Internal error: this is a defect in triauth-js - please report it at ${ISSUES_URL} together with the stack trace`,
        {cause: err}
      );

      return {
        error: {
          code: 'internal_error',
          message: 'Internal error'
        }
      };

    }
  }
 
}