mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
refactor: handle potential errors during ky requests in koa-auth middleware (#6112)
This commit is contained in:
parent
b52609a1ed
commit
75c0468abe
1 changed files with 11 additions and 0 deletions
|
@ -6,6 +6,7 @@ import type { JWK } from 'jose';
|
||||||
import { createLocalJWKSet, jwtVerify } from 'jose';
|
import { createLocalJWKSet, jwtVerify } from 'jose';
|
||||||
import type { MiddlewareType, Request } from 'koa';
|
import type { MiddlewareType, Request } from 'koa';
|
||||||
import type { IMiddleware, IRouterParamContext } from 'koa-router';
|
import type { IMiddleware, IRouterParamContext } from 'koa-router';
|
||||||
|
import { HTTPError } from 'ky';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { EnvSet } from '#src/env-set/index.js';
|
import { EnvSet } from '#src/env-set/index.js';
|
||||||
|
@ -106,6 +107,16 @@ export const verifyBearerTokenFromRequest = async (
|
||||||
throw error;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
throw new RequestError({ code: 'auth.unauthorized', status: 401 }, error);
|
throw new RequestError({ code: 'auth.unauthorized', status: 401 }, error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue