Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support AggregateError #69

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger";
import { config } from "dotenv";
import { CommentHandler } from "./comment";
import { Context } from "./context";
import { transformError } from "./error";
import { customOctokit } from "./octokit";
import { verifySignature } from "./signature";
import { inputSchema } from "./types/input-schema";
Expand Down Expand Up @@ -96,16 +97,12 @@ export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TCo
} catch (error) {
console.error(error);

let loggerError: LogReturn | null;
if (error instanceof Error) {
core.setFailed(error);
loggerError = context.logger.error(`Error: ${error}`, { error: error });
} else if (error instanceof LogReturn) {
core.setFailed(error.logMessage.raw);
loggerError = error;
} else {
core.setFailed(`Error: ${error}`);
loggerError = context.logger.error(`Error: ${error}`);
const loggerError = transformError(context, error);

if (loggerError instanceof LogReturn) {
core.setFailed(loggerError.logMessage.diff);
} else if (loggerError instanceof Error) {
core.setFailed(loggerError);
}

if (pluginOptions.postCommentOnError && loggerError) {
Expand Down
28 changes: 28 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { LogReturn } from "@ubiquity-os/ubiquity-os-logger";
import { Context } from "./context";

export function transformError(context: Context, error: unknown) {
let loggerError: LogReturn | Error;
if (error instanceof AggregateError) {
loggerError = context.logger.error(
error.errors
.map((err) => {
if (err instanceof LogReturn) {
return err.logMessage.raw;
} else if (err instanceof Error) {
return err.message;
} else {
return err;
}
})
.join("\n\n"),
{ error }
);
} else if (error instanceof Error || error instanceof LogReturn) {
loggerError = error;
} else {
loggerError = context.logger.error(String(error));
}

return loggerError;
}
10 changes: 3 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks";
import { Value } from "@sinclair/typebox/value";
import { LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger";
import { Logs } from "@ubiquity-os/ubiquity-os-logger";
import { Hono } from "hono";
import { env as honoEnv } from "hono/adapter";
import { HTTPException } from "hono/http-exception";
import { CommentHandler } from "./comment";
import { Context } from "./context";
import { transformError } from "./error";
import { PluginRuntimeInfo } from "./helpers/runtime-info";
import { customOctokit } from "./octokit";
import { verifySignature } from "./signature";
Expand Down Expand Up @@ -101,12 +102,7 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno
} catch (error) {
console.error(error);

let loggerError: LogReturn | Error | null;
if (error instanceof Error || error instanceof LogReturn) {
loggerError = error;
} else {
loggerError = context.logger.error(`Error: ${error}`);
}
const loggerError = transformError(context, error);

if (pluginOptions.postCommentOnError && loggerError) {
await context.commentHandler.postComment(context, loggerError);
Expand Down