Skip to content

Commit

Permalink
Improve error handling for generating OpenAPI paths (#178)
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 authored Aug 27, 2024
1 parent c09c6e3 commit 126c78b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
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'.`
)
);
};

0 comments on commit 126c78b

Please sign in to comment.