mirror of
https://github.com/whyour/qinglong.git
synced 2026-10-01 05:13:50 +08:00
fix: show cron validation field errors
This commit is contained in:
@@ -28,6 +28,7 @@ const CronModal = ({
|
||||
const method = cron?.id ? 'put' : 'post';
|
||||
const payload = {
|
||||
...values,
|
||||
labels: values.labels || [],
|
||||
schedule:
|
||||
scheduleType !== ScheduleType.Normal
|
||||
? scheduleTypeMap[scheduleType]
|
||||
|
||||
+6
-6
@@ -9,8 +9,9 @@ import axios, {
|
||||
AxiosResponse,
|
||||
InternalAxiosRequestConfig,
|
||||
} from 'axios';
|
||||
import { getErrorDetails, ValidationErrorResponse } from './httpError';
|
||||
|
||||
export interface IResponseData {
|
||||
export interface IResponseData extends ValidationErrorResponse {
|
||||
code?: number;
|
||||
data?: any;
|
||||
message?: string;
|
||||
@@ -43,6 +44,7 @@ const errorHandler = function (
|
||||
const msg = error.response.data
|
||||
? error.response.data.message || error.message
|
||||
: error.response.statusText;
|
||||
const errorDetails = getErrorDetails(error.response.data);
|
||||
const responseStatus = error.response.status;
|
||||
if ([502, 504].includes(responseStatus)) {
|
||||
history.push('/error');
|
||||
@@ -60,12 +62,10 @@ const errorHandler = function (
|
||||
msg &&
|
||||
notification.error({
|
||||
message: msg,
|
||||
description: error.response?.data?.errors ? (
|
||||
description: errorDetails.length ? (
|
||||
<>
|
||||
{error.response?.data?.errors?.map((item: any) => (
|
||||
<div>
|
||||
{item.message} ({item.value})
|
||||
</div>
|
||||
{errorDetails.map((detail, index) => (
|
||||
<div key={`${index}-${detail}`}>{detail}</div>
|
||||
))}
|
||||
</>
|
||||
) : undefined,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
export interface ValidationErrorResponse {
|
||||
errors?: Array<{ message?: string; value?: unknown }>;
|
||||
validation?: Record<
|
||||
string,
|
||||
{ source?: string; keys?: string[]; message?: string }
|
||||
>;
|
||||
}
|
||||
|
||||
export const getErrorDetails = (data?: ValidationErrorResponse) => {
|
||||
const details =
|
||||
data?.errors?.map((item) =>
|
||||
item.value === undefined
|
||||
? item.message
|
||||
: `${item.message} (${String(item.value)})`,
|
||||
) || [];
|
||||
|
||||
Object.values(data?.validation || {}).forEach((validation) => {
|
||||
if (validation.keys?.length) {
|
||||
validation.keys.forEach((key) => {
|
||||
details.push(
|
||||
validation.message ? `${key}: ${validation.message}` : key,
|
||||
);
|
||||
});
|
||||
} else if (validation.message) {
|
||||
details.push(validation.message);
|
||||
}
|
||||
});
|
||||
|
||||
return details.filter((detail): detail is string => Boolean(detail));
|
||||
};
|
||||
Reference in New Issue
Block a user