From 98678f7264e71bf8e73214c141d29b0b15958c15 Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Fri, 20 Dec 2024 15:09:48 +0100 Subject: [PATCH] Show upload error messages in Logging --- .../alfa-test-utils/src/report/logging.ts | 62 ++++++++++++++++--- packages/alfa-test-utils/src/report/sip.ts | 2 +- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/packages/alfa-test-utils/src/report/logging.ts b/packages/alfa-test-utils/src/report/logging.ts index af644588..42ffed40 100644 --- a/packages/alfa-test-utils/src/report/logging.ts +++ b/packages/alfa-test-utils/src/report/logging.ts @@ -1,10 +1,11 @@ import { Array } from "@siteimprove/alfa-array"; import { Element, Query } from "@siteimprove/alfa-dom"; import { Equatable } from "@siteimprove/alfa-equatable"; -import { Result } from "@siteimprove/alfa-result"; -import { Sequence } from "@siteimprove/alfa-sequence"; +import { Iterable } from "@siteimprove/alfa-iterable"; import * as json from "@siteimprove/alfa-json"; +import { Err, Result } from "@siteimprove/alfa-result"; +import { Sequence } from "@siteimprove/alfa-sequence"; import type { Thunk } from "@siteimprove/alfa-thunk"; import { Page } from "@siteimprove/alfa-web"; @@ -18,17 +19,39 @@ import { getRuleTitle } from "./get-rule-title.js"; * * @public */ -export class Logging implements Equatable, json.Serializable { - public static of(title: string, logs?: Iterable): Logging { - return new Logging(title, Sequence.from(logs ?? [])); +export class Logging + implements Equatable, json.Serializable +{ + public static of(title: string, logs?: Iterable): Logging<"log">; + + public static of( + title: string, + severity: S, + logs?: Iterable + ): Logging; + + public static of( + title: string, + severityOrLogs?: S | Iterable, + logs?: Iterable + ): Logging { + const innerLogs: Iterable = + typeof severityOrLogs === "string" ? logs ?? [] : severityOrLogs ?? []; + + const severity = + typeof severityOrLogs === "string" ? severityOrLogs : "log"; + + return new Logging(title, Sequence.from(innerLogs), severity); } private readonly _title: string; private readonly _logs: Sequence; + private readonly _severity: S; - protected constructor(title: string, logs: Sequence) { + protected constructor(title: string, logs: Sequence, severity: S) { this._title = title; this._logs = logs; + this._severity = severity; } public get title(): string { @@ -40,9 +63,13 @@ export class Logging implements Equatable, json.Serializable { } public print(): void { - console.group(this._title); - this._logs.forEach((log) => log.print()); - console.groupEnd(); + if (this._logs.isEmpty()) { + console[this._severity](this._title); + } else { + console.group(this._title); + this._logs.forEach((log) => log.print()); + console.groupEnd(); + } } public equals(value: Logging): boolean; @@ -75,6 +102,11 @@ export namespace Logging { logs: Sequence.JSON; } + /** + * {@link https://console.spec.whatwg.org/#loglevel-severity} + */ + export type Severity = "info" | "log" | "warn" | "error"; + /** @internal */ export namespace Defaults { export const Title = "Untitled"; @@ -108,7 +140,19 @@ export namespace Logging { pageReportUrl?: Result | string ): Logging { return Logging.of("Siteimprove found accessibility issues:", [ + // Show the page title Logging.of(chalk.bold(`Page - ${pageTitle ?? Defaults.Title}`)), + + // Show any error during upload: missing or invalid credentials, etc. + ...(Err.isErr(pageReportUrl) + ? [ + Logging.of( + "The following error was encountered while uploading results to the Siteimprove Intelligence Platform:", + [Logging.of(pageReportUrl.getErr(), "error")] + ), + ] + : []), + // "This page contains X issues: URL" (if URL) // "This page contains X issues." (otherwise) Logging.of( diff --git a/packages/alfa-test-utils/src/report/sip.ts b/packages/alfa-test-utils/src/report/sip.ts index 378befe4..a242d806 100644 --- a/packages/alfa-test-utils/src/report/sip.ts +++ b/packages/alfa-test-utils/src/report/sip.ts @@ -116,7 +116,7 @@ export namespace SIP { if (status === 401) { // 401 are handled by the generic server, and we don't get custom error message return Err.of( - "Unauthorized request: the request was made with invalid credentials\n Verify your username and API key" + "Unauthorized request: the request was made with invalid credentials, verify your username and API key" ); }