当前位置:网站首页>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 !
边栏推荐
- 数学公式截图识别神器Mathpix无限使用教程
- MySQL (2) -- simple query, conditional query
- 终于搞懂什么是动态规划的
- Week 17 homework
- Element positioning of Web Automation
- Fix the memory structure of JVM in one article
- Go语言实现原理——锁实现原理
- poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
- VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
- Vision Transformer (ViT)
猜你喜欢
openresty ngx_ Lua request response
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
Negative sampling
[untitled]
Element operation and element waiting in Web Automation
YML configuration, binding and injection, verification, unit of bean
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
Masked Autoencoders Are Scalable Vision Learners (MAE)
Realize reverse proxy client IP transparent transmission
Alibaba Tianchi SQL training camp task4 learning notes
随机推荐
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
Déterminer si un arbre binaire est un arbre binaire complet
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
【经典控制理论】自控实验总结
二叉树递归套路总结
实现反向代理客户端IP透传
并查集实践
判斷二叉樹是否為完全二叉樹
基于脉冲神经网络的物体检测
openresty ngx_ Lua regular expression
Getting started stm32--gpio (running lantern) (nanny level)
Go language implementation principle -- map implementation principle
Go语言实现原理——锁实现原理
【原创】程序员团队管理的核心是什么?
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
Vision Transformer (ViT)
CJ mccullem autograph: to dear Portland
Idea rundashboard window configuration
Yiwen gets rid of the garbage collector
Initial experience | purchase and activate typora software