当前位置:网站首页>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 !
边栏推荐
- 6-axis and 9-axis IMU attitude estimation
- openresty ngx_lua请求响应
- 2.13 summary
- UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
- Practice of concurrent search
- What is the process of building a website
- 数学公式截图识别神器Mathpix无限使用教程
- Data analysis - Thinking foreshadowing
- JVM的简介
- 代码农民提高生产力
猜你喜欢
Basic knowledge of database (interview)
Go语言实现原理——Map实现原理
透彻理解JVM类加载子系统
无刷驱动设计——浅谈MOS驱动电路
Three. Js-01 getting started
fibonacci search
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
Practice of concurrent search
一文搞定class的微觀結構和指令
Three.JS VR看房
随机推荐
Matlab smooth curve connection scatter diagram
Go语言实现原理——锁实现原理
Masked Autoencoders Are Scalable Vision Learners (MAE)
一文搞定class的微观结构和指令
Go语言实现原理——Map实现原理
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
如何快速理解复杂业务,系统思考问题?
Leetcode sword finger offer brush questions - day 21
【原创】程序员团队管理的核心是什么?
3:第一章:认识JVM规范2:JVM规范,简介;
The method and principle of viewing the last modification time of the web page
Practice of concurrent search
fibonacci search
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
Three.JS VR看房
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
视频标准二三事
数据库基础知识(面试)
2:第一章:认识JVM规范1:JVM简介;
Leetcode buys and sells stocks