YMFE/yapi · error

action.payload.data.errmsg

Error message

action.payload.data.errmsg

What it means

This message middleware inspects every action carrying an HTTP response payload; when the backend returns a non-zero errcode other than 40011 (a whitelisted code), it surfaces errcode/errmsg as a message toast and throws so reducers skip processing the failed response. It is the central funnel for backend business errors reaching the frontend.

Source

Thrown at client/reducer/middleware/messageMiddleware.js:16

import { message } from 'antd';

export default () => next => action => {
  if (!action) {
    return;
  }
  if (action.error) {
    message.error((action.payload && action.payload.message) || '服务器错误');
  } else if (
    action.payload &&
    action.payload.data &&
    action.payload.data.errcode &&
    action.payload.data.errcode !== 40011
  ) {
    message.error(action.payload.data.errmsg);
    throw new Error(action.payload.data.errmsg);
  }
  return next(action);
};

View on GitHub (pinned to 59bade3a8a)

Solutions

  1. Log in again or refresh the auth token, since most errcodes stem from expired/invalid sessions
  2. Check the errmsg string to identify the backend rule violated (permissions, params, etc.)
  3. Verify the request payload matches what the backend endpoint expects
  4. If the error should not toast, add the errcode to the whitelist condition in messageMiddleware.js

Example fix

// before: raw request without token handling
const res = await axios.get('/api/project/get?id=1');
// after: refresh token on auth errcodes
if (res.data.errcode === 40011) { await refreshToken(); retry(); }
else if (res.data.errcode) { toast(res.data.errmsg); }
Defensive patterns

Strategy: try-catch

Validate before calling

function hasBizError(action){ return !!(action.payload && action.payload.data && action.payload.data.errcode && action.payload.data.errcode !== 40011); }

Type guard

function isApiPayload(p){ return p && typeof p === 'object' && p.data && typeof p.data.errcode === 'number'; }

Try / catch

try {
  const res = await dispatch(fetchProject(id));
} catch (e) {
  Message.error(e.message || '请求失败');
}

Prevention

When it happens

Trigger: Any axios-dispatched action whose response body has data.errcode truthy and !== 40011, e.g. an expired login token, insufficient permissions, or invalid params returned by a YApi API endpoint.

Common situations: Session expired (errcode 40011 handled elsewhere), hitting an API without login, plugin/backend returning business errors, backend exceptions serialized into errcode/errmsg.

Related errors


AI-assisted analysis of YMFE/yapi@59bade3a8a (2026-08-29). Data as JSON: /api/errors/8dcf33b2d6f2a9d7. Report an issue: GitHub.