net4j-exception-plugin
TypeScript icon, indicating that this package has built-in type declarations

1.1.16 • Public • Published

net4j-exception-plugin

Easily to set tip when request get exception.

import Net from 'net4j';
import { Modal } from 'antd';
import NetException, { PluginConfig } from 'net4j-exception-plugin';

// Merge plugin config to net4j config,then you can use it in every requst in net4j
declare module 'net4j' {
  interface Config extends PluginConfig {}
}

// Code to message
// Code comes from http status,response result.code,promise reject error.code
const codeMsgMap = (code) => {
  const map = {
    404: 'page not found',
    403: function() {
      // In this time, tipsComponent will not show.
      link.to('/login');
    },
    5400: '商品名重复'
  };
  return map[code];
}

const net = new Net({
  plugins: [
    otherPlugins...,
    // Exception plugin should be placed last
    new NetException({
      tipsComponent: (code, text) => {
          return Modal.error({
            title: code,
            content: text,
          }),
      defaultExceptionText: '请求异常,请重试', // Default is 'fail'
      codeMsgMap,
    }),
  ]
});

// When api/goods get exception,error modal will display;
await net.get('api/goods');

Readme

Keywords

Package Sidebar

Install

npm i net4j-exception-plugin

Weekly Downloads

21

Version

1.1.16

License

ISC

Unpacked Size

6.78 kB

Total Files

5

Last publish

Collaborators

  • forthedamn