From 126c78b6e7d0d05dd2555a02a84d3c667177e0ee Mon Sep 17 00:00:00 2001 From: Markus Blomqvist Date: Tue, 27 Aug 2024 23:31:35 +0300 Subject: [PATCH] Improve error handling for generating OpenAPI paths (#178) This adds error messages whenever generating OpenAPI paths fail for routes, helping debugging any issues in those. --- packages/next-rest-framework/src/cli/utils.ts | 30 +++++++++++++------ .../next-rest-framework/src/shared/logging.ts | 15 ++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/next-rest-framework/src/cli/utils.ts b/packages/next-rest-framework/src/cli/utils.ts index b06dd1b..d85bb35 100644 --- a/packages/next-rest-framework/src/cli/utils.ts +++ b/packages/next-rest-framework/src/cli/utils.ts @@ -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'; @@ -83,8 +83,8 @@ export const findConfig = async ({ configPath }: { configPath?: string }) => { }); } } - } catch { - // Route was not a docs handler. + } catch (e) { + logGenerateErrorForRoute(getRouteName(route), e); } }) ); @@ -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); } }) ); @@ -317,6 +317,12 @@ 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)) { @@ -324,8 +330,8 @@ export const generatePathsFromBuild = async ({ schemas = { ...schemas, ...data.schemas }; } } - } catch { - // Route was not a route handler. + } catch (e) { + logGenerateErrorForRoute(getRouteName(route), e); } }) ); @@ -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) ); @@ -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); } }) ); diff --git a/packages/next-rest-framework/src/shared/logging.ts b/packages/next-rest-framework/src/shared/logging.ts index e6271bd..a9ebfa2 100644 --- a/packages/next-rest-framework/src/shared/logging.ts +++ b/packages/next-rest-framework/src/shared/logging.ts @@ -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'.` + ) + ); +};