How to use the UnprocessableEntity function from http-errors

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

95
96
97
98
99
100
101
102
103
104
misdirectedRequest: function misdirectedRequest (message) {
  return new createError.MisdirectedRequest(message)
},

unprocessableEntity: function unprocessableEntity (message) {
  return new createError.UnprocessableEntity(message)
},

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

+ 55 other calls in file

140
141
142
143
144
145
146
147
148
149
        reject(new HttpError.UnsupportedMediaType());
        break;
    case 'TAR_ENTRY_UNSUPPORTED':
    case 'TAR_ENTRY_INVALID':
    case 'TAR_ENTRY_ERROR':
        reject(new HttpError.UnprocessableEntity());
        break;
    default:
        reject(error);
}
fork icon2
star icon1
watch icon1

110
111
112
113
114
115
116
117
118
const user = await knex.select('*').from('users').where('email', data.email).first()
const sessionToken = req.signedCookies?.token

if (!bodyLength) throw new createErrors.BadRequest('Request body empty')
if (!user) throw new createErrors.ExpectationFailed('Unregistered account')
if (sessionToken) throw new createErrors.UnprocessableEntity('Session still active, you need to log out')
if (user?.verification_code) throw new createErrors.UnprocessableEntity('Account need to verification')

const verifyPassword = await argon2.verify(user.password, data.password)
fork icon4
star icon2
watch icon0

36
37
38
39
40
41
42
43
44
45
46
  return Jwt.decode(token)
}


// Blocks JWT Token from cache
function block(token) {
  if (!token) throw Error.UnprocessableEntity('Token is undefined.')
  const decoded = Jwt.decode(token)
  const key = `${config.jwt.cache_prefix}${token}`
  if (decoded.exp) {
    const expiration = decoded.exp - Math.floor(Date.now() / 1000)
fork icon1
star icon3
watch icon0

160
161
162
163
164
165
166
167
168
169
module.exports.UnsupportedMediaType = new HTTPErrors.UnsupportedMediaType()
module.exports.RangeNotSatisfiable = new HTTPErrors.RangeNotSatisfiable()
module.exports.ExpectationFailed = new HTTPErrors.ExpectationFailed()
module.exports.ImATeapot = new HTTPErrors.ImATeapot()
module.exports.MisdirectedRequest = new HTTPErrors.MisdirectedRequest()
module.exports.UnprocessableEntity = new HTTPErrors.UnprocessableEntity()
module.exports.Locked = new HTTPErrors.Locked()
module.exports.FailedDependency = new HTTPErrors.FailedDependency()
module.exports.UnorderedCollection = new HTTPErrors.UnorderedCollection()
module.exports.UpgradeRequired = new HTTPErrors.UpgradeRequired()
fork icon1
star icon0
watch icon7

+ 41 other calls in file

24
25
26
27
28
29
30
31
32
33
const login = async (req, res, next) => {
  try {
    const validatedResult = await authSchema.validateAsync(req.body);

    const user = await User.findOne({ email: validatedResult.email });
    if (!user) throw createError.UnprocessableEntity('Incorrect email or password. ');

    const isMatch = await user.validPassword(validatedResult.password);

    if (!isMatch) throw createError.Unauthorized('Email or password is invalid');
fork icon0
star icon1
watch icon0

26
27
28
29
30
31
32
33
34
 * user input validation
 */
await addCategorySchema.validateAsync(req.body);

/** check if category already exists */
if (await categoryModel.findOne({title})) throw createError.UnprocessableEntity("این دسته بندی از پیش وجود دارد");

/** get parent category */
const parentCategory = (parent && (parent !== "" || parent !== " ")) ? await categoryModel.findById(parent) : undefined;
fork icon0
star icon0
watch icon1

+ 4 other calls in file

115
116
117
118
119
120
121
122
123
const user = await getUserByIdModels(false, [data.email], 'email = $1')
const sessionToken = req.signedCookies?.token

if (!bodyLength) throw new createErrors.BadRequest('Request body empty!')
if (!user) throw new createErrors.ExpectationFailed('Unregistered account!')
if (sessionToken) throw new createErrors.UnprocessableEntity('Session still active, you need to log out!')
if (user?.verification_code !== '') throw new createErrors.UnprocessableEntity('Account need to verification!')

const verifyPassword = await argon2.verify(user?.password, data.password)
fork icon0
star icon0
watch icon0

+ 11 other calls in file

23
24
25
26
27
28
29
30
31
32

  if (!ok && validate.errors) {
    const [error] = validate.errors
    const errorMessage = `${error.instancePath} ${error.message}`

    return next(new httpErrors.UnprocessableEntity(errorMessage))
  }

  next()
}
fork icon0
star icon0
watch icon0

23
24
25
26
27
28
29
30
31
32
  }
},
put: async (req, res, next) => {
  try {
    if (!req.params.id)
      throw createError.UnprocessableEntity("Todo id is required.");
    const data = await updateTodoValidationSchema.validateAsync(req.body);
    const todo = await Todo.findByIdAndUpdate(req.params.id, data, {
      returnOriginal: false,
    });
fork icon0
star icon0
watch icon0

+ 2 other calls in file

27
28
29
30
31
32
33
34
35
36
  }
},
put: async (req, res, next) => {
  try {
    if (!req.params.id)
      throw createError.UnprocessableEntity("Post id is required.");
    const data = await updatePostValidationSchema.validateAsync(req.body);
    const post = await Post.findByIdAndUpdate(req.params.id, data, {
      returnOriginal: false,
    });
fork icon0
star icon0
watch icon0

+ 3 other calls in file

214
215
216
217
218
219
220
221
222
223
if (!sess) throw createError.Unauthorized('Login to your account')

const passwordMatch = await bcrypt.compare(sess.password, user.passwordHash)

if (!passwordMatch)
  throw createError.UnprocessableEntity('Credential and session mismatch')

const response = schema.signupSchema.safeParse(req.body)

if (!response.success) {
fork icon0
star icon0
watch icon0

129
130
131
132
133
134
135
136
137
138
case 421: {
  httpError = createError.MisdirectedRequest();
  break;
}
case 422: {
  httpError = createError.UnprocessableEntity();
  break;
}
case 423: {
  httpError = createError.Locked();
fork icon0
star icon0
watch icon2

+ 41 other calls in file