Last active
October 14, 2017 02:46
-
-
Save shahzadns/cf37c2a8edc2612820219a0430565fa1 to your computer and use it in GitHub Desktop.
Provides response format for RESTful APIs for both success and error scenarios.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created on: Oct 14 2017 | |
* Created by: Shahzad Nawaz | |
* Derived from different sources, Stakoverflow, Blogs, experience. | |
* For more advanced handling - checkout https://github.com/adnan-kamili/rest-api-response-format | |
*/ | |
// success response example - http status code - always 200 | |
var res = { | |
code: '200', | |
messages: ['signup success'], | |
data: { | |
user: user | |
} | |
}; | |
// error response example - http status code - | |
// client-side - 400 (payload missing), 401(invalid payload), 403 (unauthorized), | |
// server-side - 500 (internal error - script code breaks), 503 (service unavailable - server is down) | |
var res = { | |
code: 'ESU1', //Error Sign Up - email already exist, and similar stuff. (FE should map the given error) | |
messages: ['sign-up failed', 'email exists'] | |
}; | |
// upload excel file error example - or any similar stuff that can have multiple errors (other example. BE validation) | |
var res = { | |
code: 'ERU1', //Error Records Upload - partial success - (FE should map the given error) | |
messages: ['line#54 failed', 'line#63 failed', 'line#100 failed'] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment