Skip to content

Commit

Permalink
Merge pull request #191 from bluecadet/fix/zod-logs
Browse files Browse the repository at this point in the history
more verbose error logging
  • Loading branch information
claytercek authored Nov 26, 2024
2 parents 14d4eec + 879d4de commit 5c06cda
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilled-badgers-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bluecadet/launchpad-content": patch
"@bluecadet/launchpad-cli": patch
---

More verbose error logging, plus better messages on source configs with unions
26 changes: 10 additions & 16 deletions packages/cli/src/utils/command-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import { LogManager, type Logger, TTY_FIXED_END } from "@bluecadet/launchpad-utils";
import { LogManager, type Logger, NO_TTY, TTY_FIXED_END } from "@bluecadet/launchpad-utils";
import chalk from "chalk";
import { ResultAsync, err, errAsync, ok } from "neverthrow";
import { ZodError } from "zod";
Expand Down Expand Up @@ -74,26 +74,20 @@ function logFullErrorChain(logger: Logger | Console, error: Error) {
`${chalk.red("┌─")} ${chalk.red.bold(currentError.name)}: ${chalk.red(getPrettyMessage(currentError))}`,
);
const callstack = currentError.stack;
// logger.error(`${chalk.red(callstack ? '│' : '└')} `);

if (callstack) {
logger.error(`${chalk.red("│")}`);

const lines = callstack.split("\n").slice(1);
// log up to 3 lines of the callstack
let loggedLines = 0;

for (const line of lines) {
const isLastLine = loggedLines === lines.length - 1 || loggedLines > 2;
logger.error(
`${chalk.red("│")} ${chalk.red.dim(isLastLine && lines.length > 3 ? "..." : line.trim())}`,
);
if (isLastLine) {
logger.error(`${chalk.red("└──────────────────")}`);
}
loggedLines++;

if (loggedLines > 3) {
break;
}
logger.error(`${chalk.red("│")} ${chalk.red.dim(line)}`);
}

logger.error(`${chalk.red("│")}`);
logger.error(`${chalk.red("└──────────────────")}`);
}

if (currentError.cause && currentError.cause instanceof Error) {
currentError = currentError.cause;
logger.error(` ${chalk.red.dim("│")} ${chalk.red.dim("Caused by:")}`);
Expand Down
42 changes: 27 additions & 15 deletions packages/content/src/sources/contentful-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@ import { fetchPaginated } from "../utils/fetch-paginated.js";
import { defineSource } from "./source.js";

// If deliveryToken is provided, then previewToken is optional.
const contentfulCredentialsSchema = z.union([
z.object({
/** Content delivery token (all published content). */
deliveryToken: z.string().describe("Content delivery token (all published content)."),
/** Content preview token (only unpublished/draft content). */
previewToken: z
.string()
.optional()
.describe("Content preview token (only unpublished/draft content)."),
}),
z.object({
/** Content preview token (only unpublished/draft content). */
previewToken: z.string().describe("Content preview token (only unpublished/draft content)."),
}),
]);
const contentfulCredentialsSchema = z.union(
[
z.object({
/** Content delivery token (all published content). */
deliveryToken: z.string().describe("Content delivery token (all published content)."),
/** Content preview token (only unpublished/draft content). */
previewToken: z
.string()
.optional()
.describe("Content preview token (only unpublished/draft content)."),
}),
z.object({
/** Content preview token (only unpublished/draft content). */
previewToken: z.string().describe("Content preview token (only unpublished/draft content)."),
}),
],
{
errorMap: (error) => {
if (error.code === "invalid_union")
return {
message: "You must provide either a `deliveryToken` or a `previewToken`.",
};

return { message: error.message ?? "" };
},
},
);

const contentfulSourceSchema = z
.object({
Expand Down
36 changes: 24 additions & 12 deletions packages/content/src/sources/strapi-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ import { z } from "zod";
import { fetchPaginated } from "../utils/fetch-paginated.js";
import { defineSource } from "./source.js";

const strapiCredentialsSchema = z.union([
z.object({
/** Username or email. Should be configured via `./.env.local` */
identifier: z.string().describe("Username or email. Should be configured via `./.env.local`"),
/** Password. Should be configured via `./.env.local` */
password: z.string().describe("Password. Should be configured via `./.env.local`"),
}),
z.object({
/** A previously generated JWT token. */
token: z.string().describe("A previously generated JWT token."),
}),
]);
const strapiCredentialsSchema = z.union(
[
z.object({
/** Username or email. Should be configured via `./.env.local` */
identifier: z.string().describe("Username or email. Should be configured via `./.env.local`"),
/** Password. Should be configured via `./.env.local` */
password: z.string().describe("Password. Should be configured via `./.env.local`"),
}),
z.object({
/** A previously generated JWT token. */
token: z.string().describe("A previously generated JWT token."),
}),
],
{
errorMap: (error) => {
if (error.code === "invalid_union")
return {
message: "Either `identifier` and `password` OR a `token` must be provided.",
};

return { message: error.message ?? "" };
},
},
);

const strapiSourceSchema = z
.object({
Expand Down

0 comments on commit 5c06cda

Please sign in to comment.