All files / src/errors misc-errors.ts

100% Statements 25/25
100% Branches 5/5
100% Functions 10/10
100% Lines 24/24

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282                                                                                5x     5x 5x 5x 5x                                                                         2x 2x 2x 2x                                                           2x 2x 2x                                                     4x 4x                                                   3x 3x                                                 5x 5x                                                   4x 4x                                                 1x 1x                                                 1x 1x      
import { BaseError } from './base-error.ts';
 
/**
 * Thrown when saving an instance of a model with `version: true` updates no row because another writer changed it first (optimistic locking).
 *
 * The failed update is not retried; reload the instance and apply the change again.
 *
 * @example
 * ```ts
 * const user = await User.findByPk(1);
 * try {
 *   await user!.update({ name: 'New name' });
 * } catch (e) {
 *   if (e instanceof OptimisticLockError) {
 *     console.error(e.modelName, e.where);
 *   }
 * }
 * ```
 */
export class OptimisticLockError extends BaseError {
  /** Name of the model whose row was stale; `undefined` when not given. */
  readonly modelName: string | undefined;
  /** Primary key values of the row. */
  readonly where: unknown;
  /** Expected version values. */
  readonly values: unknown;
 
  /**
   * Builds the error; the message defaults to `Attempting to update a stale model instance: <modelName>`.
   *
   * @param options - Model name, values and where of the failed update.
   */
  constructor(
    options: Readonly<{
      message?: string;
      modelName?: string;
      values?: unknown;
      where?: unknown;
    }> = {},
  ) {
    super(
      options.message ?? `Attempting to update a stale model instance: ${options.modelName ?? ''}`,
    );
    this.name = 'SequelizeOptimisticLockError';
    this.modelName = options.modelName;
    this.values = options.values;
    this.where = options.where;
  }
}
 
/**
 * Failure of one record in a bulk operation, carrying the offending record.
 *
 * Collected into an {@link AggregateError} by `bulkCreate` with `validate: true`.
 *
 * @example
 * ```ts
 * try {
 *   await User.bulkCreate([{ email: 'not-an-email' }], { validate: true });
 * } catch (e) {
 *   if (e instanceof AggregateError) {
 *     for (const item of e.errors) {
 *       if (item instanceof BulkRecordError) {
 *         console.error(item.record, item.errors.message);
 *       }
 *     }
 *   }
 * }
 * ```
 */
export class BulkRecordError extends BaseError {
  /** Underlying error. */
  readonly errors: Error;
  /** Record that failed. */
  readonly record: object;
 
  /**
   * Wraps the validation error of one record; the message is copied from it.
   *
   * @param error - Underlying error.
   * @param record - Record that failed.
   */
  constructor(error: Error, record: object) {
    super(error.message);
    this.name = 'SequelizeBulkRecordError';
    this.errors = error;
    this.record = record;
  }
}
 
/**
 * Bundles the errors of one bulk operation into a single error (Sequelize v6 `AggregateError`).
 *
 * `bulkCreate` with `validate: true` validates every record and throws this error with one {@link BulkRecordError} per invalid record. The message joins the messages of all errors with newlines.
 *
 * @example
 * ```ts
 * try {
 *   await User.bulkCreate([{ email: 'a' }, { email: 'b' }], { validate: true });
 * } catch (e) {
 *   if (e instanceof AggregateError) {
 *     console.error(`${e.errors.length} records failed`);
 *   }
 * }
 * ```
 */
export class AggregateError extends BaseError {
  /** Collected errors. */
  readonly errors: readonly Error[];
 
  /**
   * Creates the error from the collected errors.
   *
   * @param errors - Collected errors.
   */
  constructor(errors: readonly Error[]) {
    super(errors.map((error) => error.message).join('\n'));
    this.name = 'AggregateError';
    this.errors = errors;
  }
}
 
/**
 * Thrown by finders called with `rejectOnEmpty: true` when no row matches.
 *
 * Pass an `Error` instance as `rejectOnEmpty` to throw it instead of this class.
 *
 * @example
 * ```ts
 * try {
 *   await User.findOne({ where: { email: 'nobody@example.com' }, rejectOnEmpty: true });
 * } catch (e) {
 *   if (e instanceof EmptyResultError) {
 *     console.error('no such user');
 *   }
 * }
 * ```
 */
export class EmptyResultError extends BaseError {
  /**
   * Creates the error.
   *
   * @param message - Error message.
   */
  constructor(message?: string) {
    super(message);
    this.name = 'SequelizeEmptyResultError';
  }
}
 
/**
 * Thrown when an instance method is used on an instance in the wrong state, e.g. `reload()` for a row that no longer exists.
 *
 * @example
 * ```ts
 * try {
 *   await user.destroy();
 *   await user.reload();
 * } catch (e) {
 *   if (e instanceof InstanceError) {
 *     console.error(e.message);
 *   }
 * }
 * ```
 */
export class InstanceError extends BaseError {
  /**
   * Creates the error.
   *
   * @param message - Error message.
   */
  constructor(message: string) {
    super(message);
    this.name = 'SequelizeInstanceError';
  }
}
 
/**
 * Thrown when an `include` names a model that is not associated with the queried model.
 *
 * @example
 * ```ts
 * try {
 *   await User.findAll({ include: [Tag] });
 * } catch (e) {
 *   if (e instanceof EagerLoadingError) {
 *     console.error(e.message);
 *   }
 * }
 * ```
 */
export class EagerLoadingError extends BaseError {
  /**
   * Creates the error.
   *
   * @param message - Error message.
   */
  constructor(message: string) {
    super(message);
    this.name = 'SequelizeEagerLoadingError';
  }
}
 
/**
 * Thrown when an association is defined incorrectly, e.g. two associations of one model share an alias.
 *
 * @example
 * ```ts
 * try {
 *   User.hasMany(Post, { as: 'items' });
 *   User.hasMany(Task, { as: 'items' });
 * } catch (e) {
 *   if (e instanceof AssociationError) {
 *     console.error(e.message);
 *   }
 * }
 * ```
 */
export class AssociationError extends BaseError {
  /**
   * Creates the error.
   *
   * @param message - Error message.
   */
  constructor(message: string) {
    super(message);
    this.name = 'SequelizeAssociationError';
  }
}
 
/**
 * Base class of query construction errors (Sequelize v6 `QueryError`); postgrelize does not throw it itself.
 *
 * @example
 * ```ts
 * try {
 *   await User.findAll();
 * } catch (e) {
 *   if (e instanceof QueryError) {
 *     console.error(e.message);
 *   }
 * }
 * ```
 */
export class QueryError extends BaseError {
  /**
   * Creates the error and sets `name` to `'SequelizeQueryError'`.
   *
   * @param message - Error message.
   */
  constructor(message?: string) {
    super(message);
    this.name = 'SequelizeQueryError';
  }
}
 
/**
 * Thrown for an invalid scope definition or use (Sequelize v6 `SequelizeScopeError`).
 *
 * @example
 * ```ts
 * try {
 *   User.scope('missing');
 * } catch (e) {
 *   if (e instanceof SequelizeScopeError) {
 *     console.error(e.message);
 *   }
 * }
 * ```
 */
export class SequelizeScopeError extends BaseError {
  /**
   * Creates the error and sets `name` to `'SequelizeScopeError'`.
   *
   * @param message - Error message.
   */
  constructor(message?: string) {
    super(message);
    this.name = 'SequelizeScopeError';
  }
}