How to use http-status-codes

Comprehensive http-status-codes code examples:

How to use http-status-codes.UNAUTHORIZED:

1
2
3
4
5
6
7
8
9
10
11
12
const StatusCodes = require('http-status-codes');


class UnauthenticatedError extends CustomApiError {
  constructor(message){
    super(message);
    this.statusCode = StatusCodes.UNAUTHORIZED;
  }
}


module.exports = UnauthenticatedError;

How to use http-status-codes.getStatus:

444
445
446
447
448
449
450
451
452
if (res.headersSent) {

    return next(err);
}
respond(res.status(...[
    HttpStatus.getStatus(...[
        err.code
    ]) || 500
]), {

How to use http-status-codes.FORBIDDEN:

199
200
201
202
203
204
205
206
207
208
if (index < 0 || index >= self[rc].length) return callback(new Error("Edit Review Comment, Index out of Range"));


if (self[rc][index].user !== user.OSMUser) {
  const error = new Error(">" + user.OSMUser + "< is not allowed to change review");
  error.status = HttpStatus.FORBIDDEN;
  return callback(error);
}

// nothing to change.

How to use http-status-codes.NOT_FOUND:

1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
function flushInternalCache() {
  internalCache.flushAll();
}


function grpcErrorToHTTPCode(grpcErrorCode) {
  if (grpcErrorCode == 2) return HttpStatus.NOT_FOUND;


  return HttpStatus.INTERNAL_SERVER_ERROR;
}

How to use http-status-codes.getReasonPhrase:

49
50
51
52
53
54
55
56
57
58

// message
if (message) {
  obj.message = message;
} else {
  obj.message = httpStatusCodes.getReasonPhrase(obj.code);
}

// data
if (data) obj.data = data;

How to use http-status-codes.BAD_REQUEST:

87
88
89
90
91
92
93
94
95
96
97
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
  logger.logFullError(err, req.signature || `${req.method} ${req.url}`);
  const errorResponse = {};
  let status = err.isJoi
    ? HttpStatus.BAD_REQUEST
    : err.httpStatus || _.get(err, "response.status") || HttpStatus.INTERNAL_SERVER_ERROR;


  // Check if err is a GrpcError
  if (err.details != null && err.code != null) {

How to use http-status-codes.getStatusText:

26
27
28
29
30
31
32
33
34
35
} = req.body;

if (!email || !name) {
    return res.status(BAD_REQUEST).send({
        status: FAIL,
        message: HttpStatus.getStatusText(BAD_REQUEST),
    });
}

const newUser = new User({

How to use http-status-codes.INTERNAL_SERVER_ERROR:

88
89
90
91
92
93
94
95
96
97
98
app.use((err, req, res, next) => {
  logger.logFullError(err, req.signature || `${req.method} ${req.url}`);
  const errorResponse = {};
  let status = err.isJoi
    ? HttpStatus.BAD_REQUEST
    : err.httpStatus || _.get(err, "response.status") || HttpStatus.INTERNAL_SERVER_ERROR;


  // Check if err is a GrpcError
  if (err.details != null && err.code != null) {
    status = err.code == 5 ? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST; // TODO: Use @topcoder-framework/lib-common to map GrpcError codes to HTTP codes

How to use http-status-codes.default:

111
112
113
114
115
116
117
118
119
120
function appendClipToWord(req, res, next) {
    return __awaiter(this, void 0, void 0, function* () {
        if (typeof req.file === "undefined") {
            return next({
                code: http_status_codes_1.default.UNPROCESSABLE_ENTITY,
                msg: http_status_codes_1.default.getStatusText(http_status_codes_1.default.UNPROCESSABLE_ENTITY),
            });
        }
        const word = yield (prismadb_1.default === null || prismadb_1.default === void 0 ? void 0 : prismadb_1.default.clip.create({
            data: {

How to use http-status-codes.StatusCodes:

1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
}
else {
	const statusCode = response.status;
	gthat.log.debug(`Received response from: ${url} (status code: ${statusCode} - ${response.statusText})`);

	if (statusCode != HttpStatus.StatusCodes.OK) {
		gthis.airInfo = null;
		gthat.log.error(`Invalid HTTP status code (${statusCode} - ${response.statusText}). Getting device data failed!`);
		return;
	}