Skip to content

Instantly share code, notes, and snippets.

@shahzadns
Last active October 14, 2017 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahzadns/cf37c2a8edc2612820219a0430565fa1 to your computer and use it in GitHub Desktop.
Save shahzadns/cf37c2a8edc2612820219a0430565fa1 to your computer and use it in GitHub Desktop.
Provides response format for RESTful APIs for both success and error scenarios.
/**
* 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