当前位置:网站首页>How to design API return code (error code)?
How to design API return code (error code)?
2022-07-05 23:18:00 【Java technology stack】
Click on the official account ,Java dried food Timely delivery
source :https://ken.io/note/api-errorcode-or-resultcode-desgin
One 、 Preface
Two 、HTTP Status code reference
With HTTP Take the status code as an example , In order to more clearly express and distinguish the meaning of status code ,HTTP The state is segmented .
piecewise | Segment description |
---|---|
1XX | Information , The server receives the request , Requester is required to continue |
2XX | success , Operation received and processed successfully |
3XX | Redirect , Further action is required to complete the request |
4XX | Client error , The request contains a syntax error or could not be completed |
5XX | Server error , The server encountered an error while processing the request |
For back-end development , What we usually see is :
2XX Status code , such as 200-> The request is successful ,
5XX Status code , such as 502-> Server exception , Usually, the service is not running properly , Or code execution error
The cause of the problem can be preliminarily determined through the status code ,HTTP The design idea of state is worth learning .
3、 ... and 、 Parameter conventions
Although it is a return code design , But only code You can't , There should also be corresponding message, So that people can understand
Field | type | explain |
---|---|---|
code | int | Return code |
message | string | Return code description |
Reference resources HTTP The idea of status code , We segment the error code
Return code value | explain |
---|---|
0 | success |
99999 | An unknown exception occurred in the system |
10000-19999 | Parameter check error |
20000-29999 | A Step execution failed |
30000-39999 | B Step execution failed |
Through this design , It is very convenient to distinguish between programs and people API Return result of , The key is unity ! Want to be an architect , This The architect's Atlas suggests taking a look , Little detours .
Four 、 Individualization Message
Usually ours message It's all for Engineers , But in different scenarios , The same mistake , Users may need to see different error prompts . Click on the official account ,Java dried food Timely delivery
For example 20000-29999 Indicates that the order creation failed :
application_id | code | message |
---|---|---|
100001 | 20001 | I'm sorry , You have an order in progress , Please go to my order list to process . |
100001 | 20002 | I'm sorry , You have an order in progress , Please go to my order list to process . |
Then when the request processing is finished and is about to return , according to application_id+code, To match and replace message.

So we can make the phone APP Users of 、 Users of wechat applet 、 Business users who place orders on the web page see different messages . The latest interview questions have been sorted out , You can Java Interview library applet online brush questions .
5、 ... and 、 Unified processing of return information
With a unified code, We can go through Nginx perhaps APM Tool Statistics API request Code Quantity and distribution information .
We can according to the unit time 99999 To do sth API Abnormal alarm of
We can use Code Return pie chart , Help us discover the system 、 Problems in business processes
wait , All in all , Good return code design , Can help us improve communication efficiency , Reduce the maintenance cost of the code .
Good start ! Reissue 10,000 Red envelope cover
2021 What happened in 10 It's a technical event !!
23 Design mode and Practice ( Very comprehensive )
replace Log4j2!tinylog Born in the sky
Based on a Spring Boot The fairy's private life project
Explode !Java Xie Cheng is coming !
Weigh on Guan Xuan :Redis Here comes the object mapping framework !!
Recommend a code artifact , Save at least half the amount of code !
Spring Boot 3.0 M1 Release , Formal abandonment Java 8
Spring Boot Learning notes , This is so complete !
Focus on Java Technology stack, see more dry goods
obtain Spring Boot Practical notes !
边栏推荐
- Selenium+pytest automated test framework practice
- 媒体查询:引入资源
- Three. Js-01 getting started
- asp. Net pop-up layer instance
- Krypton Factor-紫书第七章暴力求解
- 2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
- Multi camera stereo calibration
- YML configuration, binding and injection, verification, unit of bean
- JVM的简介
- Debian 10 installation configuration
猜你喜欢
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
One article deals with the microstructure and instructions of class
进击的技术er——自动化
Three.JS VR看房
数学公式截图识别神器Mathpix无限使用教程
利用LNMP实现wordpress站点搭建
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Detailed explanation of pointer and array written test of C language
如何快速理解复杂业务,系统思考问题?
随机推荐
【Note17】PECI(Platform Environment Control Interface)
regular expression
如何快速理解复杂业务,系统思考问题?
Alibaba Tianchi SQL training camp task4 learning notes
Expectation, variance and covariance
判断二叉树是否为完全二叉树
一文搞定class的微观结构和指令
无刷驱动设计——浅谈MOS驱动电路
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Go language implementation principle -- map implementation principle
openresty ngx_lua正则表达式
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Krypton Factor purple book chapter 7 violent solution
LeetCode——Add Binary
一文搞定class的微觀結構和指令
Basic knowledge of database (interview)
leecode-学习笔记
openresty ngx_ Lua regular expression
Three.JS VR看房
(4) UART application design and simulation verification 2 - TX module design (stateless machine)