| 1 |
import { inspect } from "util"; |
| 2 |
import Difference from "./difference.js"; |
| 3 |
import "./polyfill.js"; |
| 4 |
|
| 5 |
const { isArray } = Array; |
| 6 |
const { defineProperty, entries, fromEntries } = Object; |
| 7 |
|
| 8 |
const named = (name, object) => defineProperty(object, "name", { value: name }); |
| 9 |
const mapEntries = (object, f) => fromEntries(entries(object).map(f)); |
| 10 |
// eslint-disable-next-line no-confusing-arrow |
| 11 |
const toContextError = error => |
| 12 |
isArray(error) ? error.map(toContextError) : error.context || error; |
| 13 |
|
| 14 |
export default class FixtureError extends Error { |
| 15 |
constructor(difference, { cause } = {}) { |
| 16 |
super(); |
| 17 |
|
| 18 |
// Sigh, still have to manually set the name unfortunately... |
| 19 |
named(this.constructor.name, this); |
| 20 |
|
| 21 |
this.difference = difference; |
| 22 |
|
| 23 |
// Set cause ourselves, since node < 17 has a bug where it won't show it |
| 24 |
// otherwise. Technically, we display it ourselves, but best to be defensive |
| 25 |
// in case we modify this implementation later. |
| 26 |
if (cause) this.cause = cause; |
| 27 |
} |
| 28 |
|
| 29 |
static toMessage() { |
| 30 |
return this.constructor.name; |
| 31 |
} |
| 32 |
|
| 33 |
get message() { |
| 34 |
return this.constructor.toMessage(this.difference, this.cause); |
| 35 |
} |
| 36 |
|
| 37 |
// Don't show the stack of FixtureErrors, it's irrelevant. |
| 38 |
// Instead, show the cause, if present. |
| 39 |
[inspect.custom](depth, options) { |
| 40 |
return this.cause |
| 41 |
? `${this.message.replace(/(?<=error(s?))\.$/, ":\n")}\n${inspect( |
| 42 |
toContextError(this.cause), |
| 43 |
options, |
| 44 |
)}`.replace(/\n/g, "\n ") |
| 45 |
: this.message; |
| 46 |
} |
| 47 |
|
| 48 |
static fromDifference(difference, actual) { |
| 49 |
return difference === Difference.None |
| 50 |
? false |
| 51 |
: difference.path[0] !== "threw" |
| 52 |
? new FixtureError.DifferentAST(difference) |
| 53 |
: !difference.expected |
| 54 |
? new FixtureError.UnexpectedError(difference, { cause: actual.threw }) |
| 55 |
: difference.actual |
| 56 |
? new FixtureError.DifferentError(difference, { cause: actual.threw }) |
| 57 |
: actual.ast && actual.ast.errors |
| 58 |
? new FixtureError.UnexpectedRecovery(difference, { |
| 59 |
cause: actual.ast.errors, |
| 60 |
}) |
| 61 |
: new FixtureError.UnexpectedSuccess(difference); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
Object.assign( |
| 66 |
FixtureError, |
| 67 |
mapEntries( |
| 68 |
{ |
| 69 |
DifferentError: ({ expected }) => |
| 70 |
`Expected unrecoverable error: \n\n${expected}\n\n` + |
| 71 |
`But instead encountered different unrecoverable error.`, |
| 72 |
|
| 73 |
DifferentAST: ({ message }) => message, |
| 74 |
|
| 75 |
UnexpectedError: () => `Encountered unexpected unrecoverable error.`, |
| 76 |
|
| 77 |
UnexpectedSuccess: ({ expected }) => |
| 78 |
`Expected unrecoverable error:\n\n ${expected}\n\n` + |
| 79 |
`But parsing succeeded without errors.`, |
| 80 |
|
| 81 |
UnexpectedRecovery: ({ expected }, errors) => |
| 82 |
`Expected unrecoverable error:\n\n ${expected}\n\n` + |
| 83 |
`But instead parsing recovered from ${errors.length} errors.`, |
| 84 |
}, |
| 85 |
([name, toMessage]) => [ |
| 86 |
name, |
| 87 |
named( |
| 88 |
`FixtureError.${name}`, |
| 89 |
class extends FixtureError { |
| 90 |
static toMessage = toMessage; |
| 91 |
}, |
| 92 |
), |
| 93 |
], |
| 94 |
), |
| 95 |
); |