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

refactor: handle potential errors during ky requests in koa-auth middleware #6112

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
11 changes: 11 additions & 0 deletions packages/core/src/middleware/koa-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { createLocalJWKSet, jwtVerify } from 'jose';
import type { MiddlewareType, Request } from 'koa';
import type { IMiddleware, IRouterParamContext } from 'koa-router';
import { HTTPError } from 'ky';
import { z } from 'zod';

import { EnvSet } from '#src/env-set/index.js';
Expand Down Expand Up @@ -106,6 +107,16 @@
throw error;
}

/**
* Handle potential errors when ky makes requests during validation
* This may occur when fetching OIDC configuration from the oidc-config endpoint
* `TypeError`: typically thrown when the fetch operation fails (e.g., network issues)
* `HTTPError`: thrown by ky for non-2xx responses
*/
if (error instanceof TypeError || error instanceof HTTPError) {
throw error;
}

Check warning on line 118 in packages/core/src/middleware/koa-auth/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/middleware/koa-auth/index.ts#L117-L118

Added lines #L117 - L118 were not covered by tests

throw new RequestError({ code: 'auth.unauthorized', status: 401 }, error);
}
};
Expand Down
Loading