Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor: centralize control API error code reads
Josué021262766c00
diff --git a/external-rest-api/package-lock.json b/external-rest-api/package-lock.jsonindex 54f2568c5..4030250c5 100644--- a/external-rest-api/package-lock.json+++ b/external-rest-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-external-rest-api",- "version": "0.1.125",+ "version": "0.1.126","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-external-rest-api",- "version": "0.1.125",+ "version": "0.1.126","dependencies": {"@clerum/jwt-key-policy": "file:../packages/jwt-key-policy","cors": "^2.8.5",diff --git a/external-rest-api/package.json b/external-rest-api/package.jsonindex 89411b7bb..0909094f9 100644--- a/external-rest-api/package.json+++ b/external-rest-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-external-rest-api",- "version": "0.1.125",+ "version": "0.1.126","description": "External REST API for profile, team management, and RPC token brokerage","main": "dist/main.js","engines": {diff --git a/external-rest-api/src/controlApiClient.ts b/external-rest-api/src/controlApiClient.tsindex f224322ea..f843923f2 100644--- a/external-rest-api/src/controlApiClient.ts+++ b/external-rest-api/src/controlApiClient.ts@@ -37,6 +37,19 @@ export class ControlApiError extends Error {this.body = bodythis.headers = headers}++ get code(): string | null {+ const body = this.body+ if (+ body !== null &&+ typeof body === 'object' &&+ 'error' in body &&+ typeof body.error === 'string'+ ) {+ return body.error+ }+ return null+ }}function forwardableResponseHeaders(response: Response): Record<string, string> {diff --git a/external-rest-api/src/http/passwordAdmissionError.ts b/external-rest-api/src/http/passwordAdmissionError.tsindex 14749291b..638cdb3ec 100644--- a/external-rest-api/src/http/passwordAdmissionError.ts+++ b/external-rest-api/src/http/passwordAdmissionError.ts@@ -34,11 +34,7 @@ export function sendPasswordAuthorityError(error: unknown, res: Response): boolereturn true}if (error.status === 503) {- const recoveryOutcomeUnknown =- error.body !== null &&- typeof error.body === 'object' &&- 'error' in error.body &&- error.body.error === 'recovery_outcome_unknown'+ const recoveryOutcomeUnknown = error.code === 'recovery_outcome_unknown'sendPublicPasswordAuthorityError(res,503,diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.tsindex 8e29a3241..cd50a6b45 100644--- a/external-rest-api/src/releaseManifest.ts+++ b/external-rest-api/src/releaseManifest.ts@@ -8,7 +8,7 @@ export type ReleaseManifest = {export const releaseManifest: ReleaseManifest = {releaseId: 'v0.11.0',- externalRestApiVersion: '0.1.125',+ externalRestApiVersion: '0.1.126',rpcProxyVersion: '0.1.126',desktopVersion: '0.11.0',minimumDesktopVersion: '0.1.252',diff --git a/external-rest-api/src/routes/invitations.ts b/external-rest-api/src/routes/invitations.tsindex c9531fa88..f249ef4c0 100644--- a/external-rest-api/src/routes/invitations.ts+++ b/external-rest-api/src/routes/invitations.ts@@ -138,12 +138,7 @@ async function invitationPasswordAuthFromRequest(req: Request): Promise<function isInvalidInvitationProof(error: unknown): boolean {return (- error instanceof ControlApiError &&- error.status === 400 &&- error.body !== null &&- typeof error.body === 'object' &&- 'error' in error.body &&- error.body.error === 'invalid_invitation'+ error instanceof ControlApiError && error.status === 400 && error.code === 'invalid_invitation')}diff --git a/external-rest-api/src/routes/me.ts b/external-rest-api/src/routes/me.tsindex 8ec0b29c5..246c22937 100644--- a/external-rest-api/src/routes/me.ts+++ b/external-rest-api/src/routes/me.ts@@ -151,13 +151,7 @@ export function createMeRouter(): Router {return}if (message.includes('(409)')) {- const upstreamError =- error instanceof ControlApiError &&- error.body &&- typeof error.body === 'object' &&- 'error' in error.body- ? error.body.error- : undefined+ const upstreamError = error instanceof ControlApiError ? error.code : nullif (upstreamError === 'credential_changed') {res.status(PASSWORD_CREDENTIAL_CHANGED_PUBLIC_RESPONSE.status)diff --git a/external-rest-api/src/services/invitationPasswordService.ts b/external-rest-api/src/services/invitationPasswordService.tsindex 35faa0cb8..fa2621b78 100644--- a/external-rest-api/src/services/invitationPasswordService.ts+++ b/external-rest-api/src/services/invitationPasswordService.ts@@ -25,7 +25,7 @@ function invitationPasswordDomainError(| 'expired'| 'invalid_password'| null {- const code = controlApiErrorCode(error)+ const code = error.codeif (error.status === 400 && code === 'invalid_password') return 'invalid_password'if (error.status === 401 && code === 'user_retired') return 'not_found'if (error.status === 403 && code === 'forbidden') return 'forbidden'@@ -36,20 +36,8 @@ function invitationPasswordDomainError(return null}-function controlApiErrorCode(error: ControlApiError): string | null {- if (- error.body !== null &&- typeof error.body === 'object' &&- 'error' in error.body &&- typeof error.body.error === 'string'- ) {- return error.body.error- }- return null-}-function resetPasswordDomainError(error: ControlApiError): ResetPasswordDomainError | null {- const code = controlApiErrorCode(error)+ const code = error.codeif (error.status === 400 && code === 'invalid_password') return 'invalid_password'if (error.status === 400 && code === 'invalid_invitation') return 'invalid_invitation'if (error.status === 401 && code === 'user_retired') return 'invalid_invitation'diff --git a/external-rest-api/src/services/invitationsService.ts b/external-rest-api/src/services/invitationsService.tsindex 287542832..7c9188807 100644--- a/external-rest-api/src/services/invitationsService.ts+++ b/external-rest-api/src/services/invitationsService.ts@@ -6,18 +6,6 @@ type AuthContext = {sessionToken: string}-function controlApiErrorCode(error: ControlApiError): string | null {- if (- error.body !== null &&- typeof error.body === 'object' &&- 'error' in error.body &&- typeof error.body.error === 'string'- ) {- return error.body.error- }- return null-}-function passwordAuthorityUnavailable(): ControlApiError {return new ControlApiError('Password recovery authority unavailable',@@ -123,10 +111,10 @@ export async function createDesktopAuthorization(} catch (error) {if (error instanceof ControlApiError && [429, 503].includes(error.status)) throw errorif (error instanceof ControlApiError) {- if (error.status === 404 && controlApiErrorCode(error) === 'not_found') {+ if (error.status === 404 && error.code === 'not_found') {return { error: 'not_found' }}- if (error.status === 401 && controlApiErrorCode(error) === 'invalid_password') {+ if (error.status === 401 && error.code === 'invalid_password') {return { error: 'invalid_password' }}}diff --git a/external-rest-api/test/controlApiClient.test.ts b/external-rest-api/test/controlApiClient.test.tsindex cfde01db1..b60794edf 100644--- a/external-rest-api/test/controlApiClient.test.ts+++ b/external-rest-api/test/controlApiClient.test.ts@@ -61,6 +61,19 @@ describe('controlApiClient error headers', () => {})})+describe('ControlApiError.code', () => {+ it.each([+ [{ error: 'invalid_password' }, 'invalid_password'],+ [null, null],+ [undefined, null],+ [{ error: 400 }, null],+ [{ message: 'invalid_password' }, null],+ [['invalid_password'], null],+ ])('exposes only a string structured error code from %j', (body, code) => {+ expect(new ControlApiError('private', 400, body).code).toBe(code)+ })+})+describe('controlApiClient request identity', () => {afterEach(() => vi.unstubAllGlobals())