|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import type { IReporterEventEnvelope } from '../events/IReporterEventEnvelope'; |
| 5 | +import { RushError } from '../diagnostics/RushError'; |
| 6 | + |
| 7 | +/** |
| 8 | + * The `name` of the legacy `AlreadyReportedError` sentinel. |
| 9 | + * |
| 10 | + * @beta |
| 11 | + */ |
| 12 | +export const ALREADY_REPORTED_ERROR_NAME: 'AlreadyReportedError' = 'AlreadyReportedError'; |
| 13 | + |
| 14 | +const CORRELATION_KEY: unique symbol = Symbol('rush-reporter-correlated-diagnostic-id'); |
| 15 | + |
| 16 | +/** |
| 17 | + * The criteria that must be met before the legacy error bridge is removed. |
| 18 | + * |
| 19 | + * @remarks |
| 20 | + * The bridge is removed only in a later major once all criteria are satisfied. |
| 21 | + * |
| 22 | + * @beta |
| 23 | + */ |
| 24 | +export const LEGACY_ERROR_BRIDGE_REMOVAL_CRITERIA: readonly string[] = [ |
| 25 | + 'zero first-party AlreadyReportedError usages remain', |
| 26 | + 'plugin API migration guidance is published', |
| 27 | + 'ecosystem notice and migration time are provided' |
| 28 | +]; |
| 29 | + |
| 30 | +/** |
| 31 | + * The legacy print-then-throw sentinel error. |
| 32 | + * |
| 33 | + * @deprecated New usage is prohibited now that structured diagnostic APIs are |
| 34 | + * available. Emit a structured diagnostic and throw a `RushError` that |
| 35 | + * references its diagnostic id instead. This sentinel is recognized only so the |
| 36 | + * bridge can suppress duplicate rendering during migration. |
| 37 | + * |
| 38 | + * @beta |
| 39 | + */ |
| 40 | +export class AlreadyReportedError extends Error { |
| 41 | + public constructor(message?: string) { |
| 42 | + super(message ?? 'This error has already been reported.'); |
| 43 | + this.name = ALREADY_REPORTED_ERROR_NAME; |
| 44 | + |
| 45 | + // Restore the prototype chain, which is broken when subclassing a built-in |
| 46 | + // and compiling to CommonJS. |
| 47 | + Object.setPrototypeOf(this, AlreadyReportedError.prototype); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Returns `true` if the value is a legacy `AlreadyReportedError` sentinel. |
| 53 | + * |
| 54 | + * @beta |
| 55 | + */ |
| 56 | +export function isAlreadyReportedSentinel(error: unknown): boolean { |
| 57 | + return error instanceof Error && error.name === ALREADY_REPORTED_ERROR_NAME; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Correlates legacy sentinel errors with previously emitted diagnostics and |
| 62 | + * decides whether a failure should be rendered again. |
| 63 | + * |
| 64 | + * @remarks |
| 65 | + * Catch boundaries render only failures that are not already represented. This |
| 66 | + * bridge observes emitted diagnostics, so a legacy sentinel or a `RushError` |
| 67 | + * whose diagnostic was already emitted is suppressed rather than rendered twice. |
| 68 | + * |
| 69 | + * @beta |
| 70 | + */ |
| 71 | +export class LegacyErrorBridge { |
| 72 | + private readonly _emittedDiagnosticIds: Set<string> = new Set(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Records that a diagnostic id has been emitted. |
| 76 | + */ |
| 77 | + public recordEmittedDiagnostic(diagnosticId: string): void { |
| 78 | + this._emittedDiagnosticIds.add(diagnosticId); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Observes an event, recording emitted diagnostic ids. |
| 83 | + */ |
| 84 | + public ingest(event: IReporterEventEnvelope<unknown>): void { |
| 85 | + if (event.type === 'diagnosticEmitted') { |
| 86 | + const diagnosticId: string | undefined = (event.payload as { diagnosticId?: string }).diagnosticId; |
| 87 | + if (diagnosticId !== undefined) { |
| 88 | + this._emittedDiagnosticIds.add(diagnosticId); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Correlates a legacy sentinel error with the diagnostic id it corresponds to. |
| 95 | + */ |
| 96 | + public correlate(error: unknown, diagnosticId: string): void { |
| 97 | + if (typeof error === 'object' && error !== null) { |
| 98 | + (error as { [CORRELATION_KEY]?: string })[CORRELATION_KEY] = diagnosticId; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the diagnostic id correlated with an error, if any. |
| 104 | + */ |
| 105 | + public getCorrelatedDiagnosticId(error: unknown): string | undefined { |
| 106 | + if (typeof error === 'object' && error !== null) { |
| 107 | + return (error as { [CORRELATION_KEY]?: string })[CORRELATION_KEY]; |
| 108 | + } |
| 109 | + return undefined; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns `true` if the failure has already been represented and should not be |
| 114 | + * rendered again. |
| 115 | + */ |
| 116 | + public shouldSuppressRendering(error: unknown): boolean { |
| 117 | + if (isAlreadyReportedSentinel(error)) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + if (error instanceof RushError) { |
| 121 | + return this._emittedDiagnosticIds.has(error.diagnosticId); |
| 122 | + } |
| 123 | + const correlated: string | undefined = this.getCorrelatedDiagnosticId(error); |
| 124 | + if (correlated !== undefined) { |
| 125 | + return this._emittedDiagnosticIds.has(correlated); |
| 126 | + } |
| 127 | + return false; |
| 128 | + } |
| 129 | +} |
0 commit comments