Skip to content

Commit

Permalink
fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOlias committed Nov 5, 2023
1 parent 057e378 commit 27bd3e1
Showing 1 changed file with 60 additions and 48 deletions.
108 changes: 60 additions & 48 deletions packages/core/src/build/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,70 +203,44 @@ export class BuildService extends Emittery<BuildServiceEvents> {
}

async loadConfig() {
try {
const module = await this.viteNodeRunner.executeFile(
this.common.options.configFile
);
const rawConfig = module.config;
const resolvedConfig =
typeof rawConfig === "function" ? await rawConfig() : await rawConfig;
const result = await this.executeFile(this.common.options.configFile);

// TODO: Validate config lol

this.emit("newConfig", { config: resolvedConfig });
} catch (err) {
console.log("error while loading config", err);
if (result.error) {
this.handleViteNodeError(result);
return;
}

const rawConfig = result.exports.config;
const resolvedConfig =
typeof rawConfig === "function" ? await rawConfig() : await rawConfig;

// TODO: Validate config lol

this.emit("newConfig", { config: resolvedConfig });
}

async loadSchema() {
console.log("loaded schema ");
}

async loadIndexingFunctions({ files }: { files: string[] }) {
const results = await Promise.all(
files.map(async (file) => {
try {
const exports = await this.viteNodeRunner.executeFile(file);
return { file, exports };
} catch (error_) {
const error = parseViteNodeError(error_ as Error);
return { file, error };
}
})
);
const results = await Promise.all(files.map(this.executeFile));

const failures = results.filter(
const errorResults = results.filter(
(r): r is { file: string; error: ViteNodeError } => r.error !== undefined
);
const successes = results.filter(
if (errorResults.length > 0) {
this.handleViteNodeError(errorResults[0]);
return;
}

const successResults = results.filter(
(r): r is { file: string; exports: any } => r.exports !== undefined
);

if (failures.length > 0) {
failures.forEach(({ file, error }) => {
const verb =
error.name === "ESBuildTransformError"
? "transforming"
: error.name === "ESBuildBuildError" ||
error.name === "ESBuildContextError"
? "building"
: "executing";

this.common.logger.error({
service: "build",
msg: `Error while ${verb} ${path.relative(
this.common.options.rootDir,
file
)}`,
error,
});
});
for (const result of successResults) {
const { file, exports } = result;

this.common.errors.submitUserError({ error: failures[0].error });
}

for (const { file, exports } of successes) {
const fns = (exports?.ponder?.fns ?? []) as { name: string; fn: any }[];

const fnsForFile: Record<string, any> = {};
Expand Down Expand Up @@ -330,6 +304,44 @@ export class BuildService extends Emittery<BuildServiceEvents> {
this.emit("newHandlers", { handlers });
}

private async executeFile(file: string) {
try {
const exports = await this.viteNodeRunner.executeFile(file);
return { file, exports };
} catch (error_) {
const error = parseViteNodeError(error_ as Error);
return { file, error };
}
}

private handleViteNodeError({
file,
error,
}: {
file: string;
error: ViteNodeError;
}) {
const verb =
error.name === "ESBuildTransformError"
? "transforming"
: error.name === "ESBuildBuildError" ||
error.name === "ESBuildContextError"
? "building"
: "executing";

this.common.logger.error({
service: "build",
msg: `Error while ${verb} ${path.relative(
this.common.options.rootDir,
file
)}`,
error: error,
});

// TODO: Fix this error handling approach.
this.common.errors.submitUserError({ error });
}

buildSchema() {
try {
const userGraphqlSchema = readGraphqlSchema({
Expand Down

0 comments on commit 27bd3e1

Please sign in to comment.