master: Add INVALID_REQUEST response type, and use it when producing errors about input flags and arguments
MODIFY
packages/@romejs/core/common/bridges/MasterBridge.ts
packages/@romejs/core/common/bridges/MasterBridge.ts
@@ -58,7 +58,13 @@ export type MasterQueryResponseDiagnostics = {
diagnostics: Diagnostics;
};
+export type MasterQueryResponseInvalid = {
+ type: 'INVALID_REQUEST';
+ diagnostics: Diagnostics;
+};
+
export type MasterQueryResponse =
+ | MasterQueryResponseInvalid
| MasterQueryResponseSuccess
| MasterQueryResponseError
| MasterQueryResponseDiagnostics;

MODIFY
packages/@romejs/core/master/Master.ts
packages/@romejs/core/master/Master.ts
@@ -24,7 +24,7 @@ import {
} from '@romejs/cli-diagnostics';
import {consume, ConsumePath} from '@romejs/consume';
import {Event, EventSubscription} from '@romejs/events';
-import MasterRequest from './MasterRequest';
+import MasterRequest, {MasterRequestInvalid} from './MasterRequest';
import {
getDiagnosticsFromError,
deriveDiagnosticFromError,
@@ -618,6 +618,7 @@ export default class Master {
req.teardown(res);
+ // If the query asked for no data then strip all diagnostics and data values
if (query.noData) {
if (res.type === 'SUCCESS') {
return {
@@ -631,6 +632,11 @@ export default class Master {
type: 'DIAGNOSTICS',
diagnostics: [],
};
+ } else if (res.type === 'INVALID_REQUEST') {
+ return {
+ type: 'INVALID_REQUEST',
+ diagnostics: [],
+ };
}
}
@@ -815,6 +821,12 @@ export default class Master {
data: undefined,
markers,
};
+ } else {
+ if (err instanceof MasterRequestInvalid) {
+ return {
+ type: 'INVALID_REQUEST',
+ diagnostics,
+ };
} else {
return {
type: 'DIAGNOSTICS',
@@ -823,11 +835,14 @@ export default class Master {
}
}
}
+ }
async handleRequestError(
req: MasterRequest,
- err: Error,
+ rawErr: Error,
): Promise<undefined | Diagnostics> {
+ let err = rawErr;
+
// If we can derive diagnostics from the error then create a diagnostics printer
const diagnostics = getDiagnosticsFromError(err);
if (diagnostics !== undefined) {
@@ -844,8 +859,12 @@ export default class Master {
const printer = err;
if (req.bridge.alive) {
await printer.print();
+
+ // Don't output the footer if this is a notifier for an invalid request as it will be followed by a help screen
+ if (!(rawErr instanceof MasterRequestInvalid)) {
printer.footer();
}
+ }
return printer.getDiagnostics();
}

MODIFY
packages/@romejs/core/master/MasterRequest.ts
packages/@romejs/core/master/MasterRequest.ts
@@ -16,6 +16,7 @@ import {
DiagnosticOrigin,
PartialDiagnosticAdvice,
createSingleDiagnosticError,
+ DiagnosticsError,
} from '@romejs/diagnostics';
import {DiagnosticsPrinterFlags} from '@romejs/cli-diagnostics';
import {ProjectDefinition} from '@romejs/project';
@@ -73,6 +74,8 @@ type NormalizedCommandFlags = {
defaultFlags: Dict<unknown>;
};
+export class MasterRequestInvalid extends DiagnosticsError {}
+
export default class MasterRequest {
constructor(opts: MasterRequestOptions) {
this.query = opts.query;
@@ -199,7 +202,8 @@ export default class MasterRequest {
advice?: PartialDiagnosticAdvice,
) {
const pointer = this.getDiagnosticPointerFromFlags(target);
- throw createSingleDiagnosticError({
+ throw new MasterRequestInvalid(message, [
+ {
message,
filename: 'argv',
category:
@@ -208,7 +212,8 @@ export default class MasterRequest {
: 'flags/invalid',
...pointer,
advice,
- });
+ },
+ ]);
}
getDiagnosticPointerForClientCwd(): DiagnosticPointer {

master: Add INVALID_REQUEST response type, and use it when producing errors about input flags and arguments