Newer
Older
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// various little utilities to make crafting boilerplate responses
// simple
Lloyd Hilaiel
committed
function sendResponse(resp, content, reason, code) {
if (content) {
if (reason) content += ": " + reason;
} else if (reason) {
content = reason;
} else {
content = "";
Lloyd Hilaiel
committed
resp.send(content, {"Content-Type": "text/plain"}, code);
}
Lloyd Hilaiel
committed
Lloyd Hilaiel
committed
exports.notFound = function(resp, reason) {
sendResponse(resp, "Not Found", reason, 404);
Lloyd Hilaiel
committed
exports.serverError = function(resp, reason) {
sendResponse(resp, "Server Error", reason, 500);
Lloyd Hilaiel
committed
};
exports.serviceUnavailable = function(resp, reason) {
sendResponse(resp, "Service Unavailable", reason, 503);
};
Lloyd Hilaiel
committed
exports.authRequired = function(resp, reason) {
sendResponse(resp, "Authentication Required", reason, 401);
};
Lloyd Hilaiel
committed
exports.badRequest = function(resp, reason) {
sendResponse(resp, "Bad Request", reason, 400);
Lloyd Hilaiel
committed
exports.forbidden = function(resp, reason) {
sendResponse(resp, "Forbidden", reason, 403);
Lloyd Hilaiel
committed
exports.throttled = function(resp, reason) {
sendResponse(resp, "Too Many Requests", reason, 429);