0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-31 22:51:25 -05:00

refactor(phrases,core,console): update API and console error handling/display

This commit is contained in:
Darcy Ye 2024-03-24 23:45:28 +08:00
parent 0f35538629
commit 9e67b670de
No known key found for this signature in database
GPG key ID: B46F4C07EDEFC610
32 changed files with 157 additions and 9 deletions

View file

@ -1,8 +1,10 @@
import { type JsonObject, LogtoJwtTokenPath } from '@logto/schemas';
import { type JsonObject, LogtoJwtTokenPath, type RequestErrorBody } from '@logto/schemas';
import classNames from 'classnames';
import { HTTPError } from 'ky';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useFormContext, Controller, type ControllerRenderProps } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { z } from 'zod';
import Button from '@/ds-components/Button';
import Card from '@/ds-components/Card';
@ -27,6 +29,7 @@ type Props = {
const userTokenModelSettings = [accessTokenPayloadTestModel, userContextTestModel];
const machineToMachineTokenModelSettings = [clientCredentialsPayloadTestModel];
const testEndpointPath = 'api/configs/jwt-customizer/test';
const jwtCustomizerGeneralErrorCode = 'jwt_customizer.general';
function TestTab({ isActive }: Props) {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console.jwt_claims' });
@ -57,7 +60,21 @@ function TestTab({ isActive }: Props) {
json: formatFormDataToTestRequestPayload(payload),
})
.json<JsonObject>()
.catch((error: unknown) => {
.catch(async (error: unknown) => {
if (error instanceof HTTPError) {
const { response } = error;
const metadata = await response.clone().json<RequestErrorBody>();
if (metadata.code === jwtCustomizerGeneralErrorCode) {
const result = z.object({ message: z.string() }).safeParse(metadata.data);
if (result.success) {
setTestResult({
error: result.data.message,
});
return;
}
}
}
setTestResult({
error: error instanceof Error ? error.message : String(error),
});

View file

@ -19,6 +19,7 @@ import {
jsonObjectGuard,
} from '@logto/schemas';
import { adminTenantId } from '@logto/schemas';
import { ResponseError } from '@withtyped/client';
import { z } from 'zod';
import { EnvSet } from '#src/env-set/index.js';
@ -340,13 +341,23 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
const client = await cloudConnection.getClient();
ctx.body = await client.post(`/api/services/custom-jwt`, {
body: {
...rest,
token: tokenSample,
context: contextSample,
},
});
try {
ctx.body = await client.post(`/api/services/custom-jwt`, {
body: {
...rest,
token: tokenSample,
context: contextSample,
},
});
} catch (error: unknown) {
if (error instanceof ResponseError) {
const { message } = z.object({ message: z.string() }).parse(await error.response.json());
throw new RequestError({ code: 'jwt_customizer.general', status: 422 }, { message });
}
throw error;
}
return next();
}
);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);

View file

@ -5,6 +5,7 @@ import domain from './domain.js';
import entity from './entity.js';
import guard from './guard.js';
import hook from './hook.js';
import jwt_customizer from './jwt-customizer.js';
import localization from './localization.js';
import log from './log.js';
import oidc from './oidc.js';
@ -34,6 +35,7 @@ const errors = {
connector,
verification_code,
sign_in_experiences,
jwt_customizer,
localization,
swagger,
entity,

View file

@ -0,0 +1,6 @@
const jwt_customizer = {
/** UNTRANSLATED */
general: 'An error occurred while customizing the JWT token. Please try again later.',
};
export default Object.freeze(jwt_customizer);