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

Improve error handling for generating OpenAPI paths #178

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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
30 changes: 21 additions & 9 deletions packages/next-rest-framework/src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type NextRestFrameworkConfig } from '../types';
import { existsSync, readdirSync } from 'fs';
import { type OpenAPIV3_1 } from 'openapi-types';
import { join } from 'path';
import { isValidMethod } from '../shared';
import { isValidMethod, logGenerateErrorForRoute } from '../shared';
import { merge } from 'lodash';
import { OPEN_API_VERSION } from './constants';

Expand Down Expand Up @@ -83,8 +83,8 @@ export const findConfig = async ({ configPath }: { configPath?: string }) => {
});
}
}
} catch {
// Route was not a docs handler.
} catch (e) {
logGenerateErrorForRoute(getRouteName(route), e);
}
})
);
Expand Down Expand Up @@ -128,8 +128,8 @@ export const findConfig = async ({ configPath }: { configPath?: string }) => {
config: _config
});
}
} catch {
// API route was not a docs handler.
} catch (e) {
logGenerateErrorForRoute(getApiRouteName(route), e);
}
})
);
Expand Down Expand Up @@ -317,15 +317,21 @@ 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) {
logGenerateErrorForRoute(getRouteName(route), e);
}
})
);
Expand Down Expand Up @@ -355,6 +361,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 +375,8 @@ export const generatePathsFromBuild = async ({
paths = { ...paths, ...data.paths };
schemas = { ...schemas, ...data.schemas };
}
} catch {
// Route was not an API route handler.
} catch (e) {
logGenerateErrorForRoute(getApiRouteName(apiRoute), e);
}
})
);
Expand Down
15 changes: 15 additions & 0 deletions packages/next-rest-framework/src/shared/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ export const logNextRestFrameworkError = (error: unknown) => {
${error}`)
);
};

export const logGenerateErrorForRoute = (path: string, error: unknown) => {
console.info(
chalk.yellow(`---
Error while importing ${path}, skipping path...`)
);

console.error(chalk.red(error));

console.info(
chalk.yellow(
`If you don't want this path to be part of your generated OpenAPI spec and want to prevent seeing this error in the future, please add ${path} to 'deniedPaths'.`
)
);
};
Loading