How to use the MethodNotAllowed function from http-errors

Find comprehensive JavaScript http-errors.MethodNotAllowed code examples handpicked from public code repositorys.

49
50
51
52
53
54
55
56
57
                                }
                        }
                        break;
                default:
                        response.set('allow', 'GET,POST');
                        next(new httpError.MethodNotAllowed());
                        break;
        }
}
fork icon51
star icon418
watch icon30

+ 7 other calls in file

35
36
37
38
39
40
41
42
43
44
notFound: function notFound (message) {
  return new createError.NotFound(message)
},

methodNotAllowed: function methodNotAllowed (message) {
  return new createError.MethodNotAllowed(message)
},

notAcceptable: function notAcceptable (message) {
  return new createError.NotAcceptable(message)
fork icon30
star icon340
watch icon17

+ 55 other calls in file

276
277
278
279
280
281
282
283
284
285
if (options.throw) {
  let notAllowedThrowable;
  if (typeof options.methodNotAllowed === 'function') {
    notAllowedThrowable = options.methodNotAllowed(); // set whatever the user returns from their function
  } else {
    notAllowedThrowable = new HttpError.MethodNotAllowed();
  }
  throw notAllowedThrowable;
} else {
  ctx.status = 405;
fork icon9
star icon49
watch icon0

50
51
52
53
54
55
56
57
58
59
60
61
}


// Renew JWT Token when is going to be expired
function renew(token, expire) {
  if (!token) throw Error.UnprocessableEntity('Token is undefined.')
  if (!config.jwt.allow_renew) throw Error.MethodNotAllowed('Renewing tokens is not allowed.')


  const decoded = Jwt.decode(token)
  if (!decoded.exp) return token
  if (decoded.exp - Math.floor(Date.now() / 1000) > config.jwt.renew_threshold) return token
fork icon1
star icon3
watch icon0

145
146
147
148
149
150
151
152
153
154
/* Generic errors */
module.exports.BadRequest = new HTTPErrors.BadRequest()
module.exports.Unauthorized = new HTTPErrors.Unauthorized()
module.exports.Forbidden = new HTTPErrors.Forbidden()
module.exports.NotFound = new HTTPErrors.NotFound()
module.exports.MethodNotAllowed = new HTTPErrors.MethodNotAllowed()
module.exports.NotAcceptable = new HTTPErrors.NotAcceptable()
module.exports.ProxyAuthenticationRequired = new HTTPErrors.ProxyAuthenticationRequired()
module.exports.RequestTimeout = new HTTPErrors.RequestTimeout()
module.exports.Conflict = new HTTPErrors.Conflict()
fork icon1
star icon0
watch icon7

+ 41 other calls in file

394
395
396
397
398
399
400
401
402
403
} else if (!allowed[ctx.method]) {
  if (options.throw) {
    const notAllowedThrowable =
      typeof options.methodNotAllowed === "function"
        ? options.methodNotAllowed() // set whatever the user returns from their function
        : new HttpError.MethodNotAllowed();

    throw notAllowedThrowable;
  } else {
    ctx.status = 405;
fork icon0
star icon2
watch icon0

22
23
24
25
26
27
28
29
30
31
bookSchema.pre("save", async function (next) {
  try {
    const checkIn = this.checkIn;
    const checkOut = this.checkOut;
    if (checkIn > checkOut)
      throw createHttpError.MethodNotAllowed("check in and checkout illegal");
    const booking = await this.constructor
      .findOne({ roomId: this.roomId })
      .populate("userId")
      .populate("roomId")
fork icon0
star icon0
watch icon0

+ 3 other calls in file

106
107
108
109
110
111
112
113
114
115
  continue;
}

if (endpoint._methods.indexOf(method) === -1) {
  error = Object.assign(
    new httperr.MethodNotAllowed(),
    {methods: endpoint._methods}
  );
  continue;
}
fork icon0
star icon0
watch icon0

368
369
370
371
372
373
374
375
376
377
    ctx.status = 200;
    ctx.body = '';
    ctx.set("Allow", allowedArr.join(", "));
} else if (!allowed[ctx.method]) {
    if (options.throw) {
        throw typeof options.methodNotAllowed === "function" ? options.methodNotAllowed() : new HttpError.MethodNotAllowed();
    } else {
        ctx.status = 405;
        ctx.set("Allow", allowedArr.join(", "));
    }
fork icon0
star icon0
watch icon0

69
70
71
72
73
74
75
76
77
78
case 404: {
  httpError = createError.NotFound();
  break;
}
case 405: {
  httpError = createError.MethodNotAllowed();
  break;
}
case 406: {
  httpError = createError.NotAcceptable();
fork icon0
star icon0
watch icon2

+ 41 other calls in file