Skip to content

Commit

Permalink
Show upload error messages in Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jym77 committed Dec 20, 2024
1 parent e72bc47 commit 98678f7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
62 changes: 53 additions & 9 deletions packages/alfa-test-utils/src/report/logging.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -18,17 +19,39 @@ import { getRuleTitle } from "./get-rule-title.js";
*
* @public
*/
export class Logging implements Equatable, json.Serializable<Logging.JSON> {
public static of(title: string, logs?: Iterable<Logging>): Logging {
return new Logging(title, Sequence.from(logs ?? []));
export class Logging<S extends Logging.Severity = Logging.Severity>
implements Equatable, json.Serializable<Logging.JSON>
{
public static of(title: string, logs?: Iterable<Logging>): Logging<"log">;

public static of<S extends Logging.Severity = "log">(
title: string,
severity: S,
logs?: Iterable<Logging>
): Logging<S>;

public static of<S extends Logging.Severity = "log">(
title: string,
severityOrLogs?: S | Iterable<Logging>,
logs?: Iterable<Logging>
): Logging<S | "log"> {
const innerLogs: Iterable<Logging> =
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<Logging>;
private readonly _severity: S;

protected constructor(title: string, logs: Sequence<Logging>) {
protected constructor(title: string, logs: Sequence<Logging>, severity: S) {
this._title = title;
this._logs = logs;
this._severity = severity;
}

public get title(): string {
Expand All @@ -40,9 +63,13 @@ export class Logging implements Equatable, json.Serializable<Logging.JSON> {
}

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;
Expand Down Expand Up @@ -75,6 +102,11 @@ export namespace Logging {
logs: Sequence.JSON<JSON>;
}

/**
* {@link https://console.spec.whatwg.org/#loglevel-severity}
*/
export type Severity = "info" | "log" | "warn" | "error";

/** @internal */
export namespace Defaults {
export const Title = "Untitled";
Expand Down Expand Up @@ -108,7 +140,19 @@ export namespace Logging {
pageReportUrl?: Result<string, string> | 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<string>(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(
Expand Down
2 changes: 1 addition & 1 deletion packages/alfa-test-utils/src/report/sip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}

Expand Down

0 comments on commit 98678f7

Please sign in to comment.