diff --git a/packages/core/client/src/api-client/APIClient.ts b/packages/core/client/src/api-client/APIClient.ts index c75deeaf3..26385c14b 100644 --- a/packages/core/client/src/api-client/APIClient.ts +++ b/packages/core/client/src/api-client/APIClient.ts @@ -4,15 +4,23 @@ import { notification } from 'antd'; import React from 'react'; import { Application } from '../application'; +function notify(type, messages, instance) { + if (!messages?.length) { + return; + } + instance[type]({ + message: messages.map?.((item: any) => { + return React.createElement('div', {}, typeof item === 'string' ? item : item.message); + }), + }); +} + const handleErrorMessage = (error, notification) => { const reader = new FileReader(); reader.readAsText(error?.response?.data, 'utf-8'); reader.onload = function () { - notification.error({ - message: JSON.parse(reader.result as string).errors?.map?.((error: any) => { - return React.createElement('div', {}, error.message); - }), - }); + const messages = JSON.parse(reader.result as string).errors; + notify('error', messages, notification); }; }; @@ -57,7 +65,20 @@ export class APIClient extends APIClientSDK { useNotificationMiddleware() { this.axios.interceptors.response.use( - (response) => response, + (response) => { + if (response.data?.messages?.length) { + const messages = response.data.messages.filter((item) => { + const lastTime = errorCache.get(typeof item === 'string' ? item : item.message); + if (lastTime && new Date().getTime() - lastTime < 500) { + return false; + } + errorCache.set(item.message, new Date().getTime()); + return true; + }); + notify('success', messages, this.notification); + } + return response; + }, (error) => { if (this.silence) { throw error; @@ -79,11 +100,10 @@ export class APIClient extends APIClientSDK { if (this.app.maintaining) { this.app.error = error?.response?.data?.error; throw error; - return; } else if (this.app.error) { this.app.error = null; } - let errs = error?.response?.data?.errors || [{ message: 'Server error' }]; + let errs = error?.response?.data?.errors || error?.response?.data?.messages || [{ message: 'Server error' }]; errs = errs.filter((error) => { const lastTime = errorCache.get(error.message); if (lastTime && new Date().getTime() - lastTime < 500) { @@ -95,11 +115,8 @@ export class APIClient extends APIClientSDK { if (errs.length === 0) { throw error; } - this.notification.error({ - message: errs?.map?.((error: any) => { - return React.createElement('div', {}, error.message); - }), - }); + + notify('error', errs, this.notification); } throw error; }, diff --git a/packages/core/server/src/middlewares/data-wrapping.ts b/packages/core/server/src/middlewares/data-wrapping.ts index a7df9cab9..4e1bf11e7 100644 --- a/packages/core/server/src/middlewares/data-wrapping.ts +++ b/packages/core/server/src/middlewares/data-wrapping.ts @@ -56,6 +56,10 @@ export function dataWrapping() { data: ctx.body, }; } + + if (ctx.body && ctx.state.messages?.length) { + ctx.body.messages = ctx.state.messages; + } }; }