httpStatus.js 684 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*jshint node:true */
"use strict";

var data = {
	created: { code: 201, body: "Created" },
	noContent: { code: 204, body: "" },
	badRequest: { code: 400, body: "Bad request" },
	unauthorized: {code: 401, body: "Unauthorized"},
	notFound: { code: 404, body: "Not found" },
	methodNotAllowed: { code: 405, body: "Method not allowed" },
	notImplemented: { code: 501, body: "Not implemented"}
};

function HTTPStatus(code, message) {
	var err = new Error(message);
	err.code = code;

	return err;
}

HTTPStatus.names = Object.keys(data);

HTTPStatus.names.forEach(function(key) {
	HTTPStatus[key] = HTTPStatus.bind(null, data[key].code, data[key].body);
});

module.exports = HTTPStatus;