REST APIs should provide clear and structured error responses to help clients understand and resolve issues effectively. Below are examples of different types of error responses that an API might return.
Single Error Responses
Incorrect Username or Password
1
2
3
4
5
6
7
{"type":"/errors/incorrect-user-pass","title":"Incorrect username or password.","status":401,"detail":"Authentication failed due to incorrect username or password.","instance":"/login/log/abc123"}
Validation Errors
Title Must Be Defined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{"data":null,"error":{"status":400,"name":"ValidationError","message":"title must be defined.","details":{"errors":[{"path":["title"],"message":"title must be defined.","name":"ValidationError"}]}}}
Name Must Be At Most 50 Characters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{"data":null,"error":{"status":400,"name":"ValidationError","message":"name must be at most 50 characters","details":{"errors":[{"path":["name"],"message":"name must be at most 50 characters","name":"ValidationError"}]}}}
This Attribute Must Be Unique
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{"data":null,"error":{"status":400,"name":"ValidationError","message":"This attribute must be unique","details":{"errors":[{"path":["name"],"message":"This attribute must be unique","name":"ValidationError"}]}}}
{"data":null,"error":{"status":400,"name":"ValidationError","message":"2 errors occurred","details":{"errors":[{"path":["name"],"message":"name must be defined.","name":"ValidationError"},{"path":["title"],"message":"title must be defined.","name":"ValidationError"}]}}}
{"placement":"field","title":"value too short","detail":"field username must be at least 4 symbols","location":"username","field":"username","code":"validation.min","expression":"min","argument":"4","traceid":"74681b27-b1ea-454d-9847-d27059e19119","stacktraces":[{"file":"model/response.go","function":"model.(*ErrorMessage).LogStacktraceWithErr","linenumber":22,"realerror":null},{"file":"helpers/validator.go","function":"helpers.Validator","linenumber":58,"realerror":null},{"file":"handlers/registration.go","function":"handlers.RegistrationHandler","linenumber":61,"realerror":null}]}
{"placement":"field","title":"must be equal","detail":"field password is not equal to field confirmpassword","location":"password","field":"password","code":"validation.equal","expression":"equal","argument":"confirmpassword","traceid":"e017ecb2-d72f-4f79-889f-6c42126970a8","stacktraces":[{"file":"model/response.go","function":"model.(*ErrorMessage).LogStacktraceWithErr","linenumber":22,"realerror":null},{"file":"helpers/validator.go","function":"helpers.Validator","linenumber":58,"realerror":null},{"file":"handlers/registration.go","function":"handlers.RegistrationHandler","linenumber":61,"realerror":null}]}
{"placement":"field","title":"incorrect email format","detail":"field email has incorrect value","location":"email","field":"email","code":"validation.email","expression":null,"argument":null,"traceid":"cecda6b8-7ce8-4054-8c06-9382320afd78","stacktraces":[{"file":"model/response.go","function":"model.(*ErrorMessage).LogStacktraceWithErr","linenumber":22,"realerror":null},{"file":"helpers/validator.go","function":"helpers.Validator","linenumber":58,"realerror":null},{"file":"handlers/registration.go","function":"handlers.RegistrationHandler","linenumber":61,"realerror":null}]}
These error responses provide a consistent structure, making it easier for clients to handle errors programmatically and display meaningful messages to users.