ZijianHe / koa-router

Router middleware for koa.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exceptions throw 404 rather than 500

cecilcosta opened this issue · comments

node.js version: v12.4.0

npm/yarn and version: npm 6.9.0

koa-router version: 3.2.0

koa version: 2.7.0

Code sample:

module.exports.mycall = async (ctx, next) => {
  const object = await ObjectFinder.find(ctx.request.body.id); // this object doesn't exits and an exception is thrown.
  object.date = new Date();
  await object.save()
  ctx.status = 204
};

Expected Behavior:

As there was an exception the response code should be 500

Actual Behavior:

Got a 404, which makes no sense.

Additional steps, HTTP request details, or to reproduce the behavior or a test case:

I have similar problem with koa-router (v. 7.4), koa(2.7) i am working with nodemailer, and catching 404 error before like mail sended for user, but want send 200 status after sending mail

`const feedback = async (ctx) => {
const {
company, username, phone, email,
} = ctx.request.body;

// here get 404, but if written ctx.status = 200; it is ok, but not sense

const mailer = async () => {
const transporter = await nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: mailuser,
pass: mailpass,
},
});

const output = `
      <p>company: ${company}</p>
      <p>username: ${username}</p>
      <p>phone: ${phone}</p>
      <p>E-mail: ${email}</p>
`;
const info = await transporter.sendMail({
  from: username,
  to: email,
  subject: email,
  html: output,
});

console.log(info.messageId);
return new Promise((res, rej) => {
if (info.messageId.length > 0) {
res();
return info;
}
rej();
return 0;
});
};

mailer().then(() => {
console.log('where is my OK status ??');
ctx.body = {
status: 200,
msg: 'ok',
};
});
};`

Log in my console

POST /sendfeed - 38ms - 404 - hostOrigin -> undefined userAgent -> PostmanRuntime/7.15.2
cookies: no cookies
9330e33a-8c8e-e962-1bfc-fd4dfd93ce8f@my-pc
where is my OK status ??