Skip to content

Commit

Permalink
Improve error handling for generating OpenAPI paths
Browse files Browse the repository at this point in the history
This adds error messages whenever generating OpenAPI
paths fail for routes, helping debugging any issues
in those.
  • Loading branch information
blomqma committed Aug 27, 2024
1 parent c09c6e3 commit 73e84db
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions packages/next-rest-framework/src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ export const findConfig = async ({ configPath }: { configPath?: string }) => {
});
}
}
} catch {
// Route was not a docs handler.
} catch (e) {
console.error(
chalk.red(`---
Error while getting Next REST Framework config from ${getRouteName(
route
)}, skipping route...
${e}`)
);
}
})
);
Expand Down Expand Up @@ -128,8 +134,14 @@ export const findConfig = async ({ configPath }: { configPath?: string }) => {
config: _config
});
}
} catch {
// API route was not a docs handler.
} catch (e) {
console.error(
chalk.red(`---
Error while getting Next REST Framework config from ${getApiRouteName(
route
)}, skipping API route...
${e}`)
);
}
})
);
Expand Down Expand Up @@ -317,15 +329,27 @@ export const generatePathsFromBuild = async ({
.map(([_key, handler]) => handler);

for (const handler of handlers) {
const isDocsHandler = !!handler._nextRestFrameworkConfig;

if (isDocsHandler) {
continue;
}

const data = await handler._getPathsForRoute(getRouteName(route));

if (isNrfOasData(data)) {
paths = { ...paths, ...data.paths };
schemas = { ...schemas, ...data.schemas };
}
}
} catch {
// Route was not a route handler.
} catch (e) {
console.error(
chalk.red(`---
Error while generating OpenAPI path for ${getRouteName(
route
)}, skipping route...
${e}`)
);
}
})
);
Expand Down Expand Up @@ -355,6 +379,12 @@ export const generatePathsFromBuild = async ({
const url = new URL(`file://${filePathToRoute}`).toString();
const res = await import(url).then((mod) => mod.default);

const isDocsHandler = !!res.default._nextRestFrameworkConfig;

if (isDocsHandler) {
return;
}

const data = await res.default._getPathsForRoute(
getApiRouteName(apiRoute)
);
Expand All @@ -363,8 +393,14 @@ export const generatePathsFromBuild = async ({
paths = { ...paths, ...data.paths };
schemas = { ...schemas, ...data.schemas };
}
} catch {
// Route was not an API route handler.
} catch (e) {
console.error(
chalk.red(`---
Error while generating OpenAPI path for ${getApiRouteName(
apiRoute
)}, skipping API route...
${e}`)
);
}
})
);
Expand Down

0 comments on commit 73e84db

Please sign in to comment.