Closed tyukesz closed 3 years ago
import "express-async-errors";
this works just fine for me, be sure to import it at the top before any routes
@tyukesz I'm afraid I don't use TypeScript at the moment so can't offer a first-hand resolution on this one. Have you had any luck with bigyank's suggestion?
@tyukesz I'm afraid I don't use TypeScript at the moment so can't offer a first-hand resolution on this one. Have you had any luck with bigyank's suggestion?
I tried and doesn't worked.
After all we implemented our own handler in typescript.
import "express-async-errors";
works perfectly fine for me in TS, ,maybe that is your fix?
i think someone requested a tsconfig file, here's mine, i hope it helps https://pastebin.com/9dpPDmU3
I am just learning TS so I can't contribute much but line 45 and 52 looks interesting
It turns out I was importing express-async-errors
too late in the program. You need to import it BEFORE you create any express.Router
objects.
Bad:
// main.ts
import express from 'express';
import {users} from './users';
import 'express-async-errors'; // too late
const app = express();
app.use('/users', users);
// users.ts
import express from 'express';
export const users = express.Router();
router.get('/me', async ...);
Good:
// main.ts
import express from 'express';
import 'express-async-errors'; // perfect
import {users} from './users';
...
It turns out I was importing
express-async-errors
too late in the program. You need to import it BEFORE you create anyexpress.Router
objects.Bad:
// main.ts import express from 'express'; import {users} from './users'; import 'express-async-errors'; // too late const app = express(); app.use('/users', users); // users.ts import express from 'express'; export const users = express.Router(); router.get('/me', async ...);
Good:
// main.ts import express from 'express'; import 'express-async-errors'; // perfect import {users} from './users'; ...
Oh, man! After all this trouble, finally found the cure.
Are you sure this works? Even though it seems to be imported ok it complains about missing types file.
Could not find a declaration file for module 'express-async-errors'. '/myproject/server/node_modules/express-async-errors/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/express-async-errors` if it exists or add a new declaration (.d.ts) file containing `declare module 'express-async-errors';`ts(7016)
It doesn't work with import statement; ReferenceError: require is not defined in ES module scope, you can use import instead
Hi, I had the same problem as tyukesz. To solve it, i imported "express-async-errors" before routes. And migrate my controller to "async", otherwise "express" does not catch errors. Sample async controller method (await is also important) :
export const user = async (req, res, next) => {
const token = req.headers['x-token'];
const response = await userRepository.getUser(token)
res.status(200).send(response);
}
in one of my project i was handling error by using throw new AnotherError("messagetype","message")
and this AnotherError is extended class of CustomError which further extended by Error class
but now in my ongoing project i am facing issue , errors are not being caught
this.app.use('/api/v1/auth',authRouter)
this.app.all('*', async (request, response, next) => {
logger?.info(request.url)
throw new NotFoundError()
})
this.app.use(ErrorHandler)
}```
in my server.ts
```export const ErrorHandler = (
error: Error,
_request: express.Request,
response: express.Response,
_next: express.NextFunction
) => {
console.error(1, error);
if (error instanceof AnotherError) {
return response.status(400).json({
status: false,
error: {
code: error.codePhrase,
message: error.message,
},
});
} else if (error instanceof NotFoundError) {
return response.status(404).json({
status: false,
error: {
code: error.codePhrase,
message: error.message,
},
});
} else {
return response.status(500).json({
status: false,
error: {
code: error.name,
message: error.message,
},
});
}
};
``` my error handler
Hi,
Recently we switched from JS to TypeScript. Everything works fine after proper configuration except this package. We tried every crazy idea which came in our mind, but nothing helped.
We are using
require('express-async-errors');
at the top of the app.ts, but it seems that errors which are thrown in async functions are not catched.Before TS everything worked, but now I am getting the following error:
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Any idea or workaround? I do not want to wrap every route.
Thanks, ty.