tsed: [BUG] UseAfter decorator does not work for error middleware
Information
- Version: 6.103.2
- Packages:
I have error middleware that should be applied for certain controllers/endpoints. It worked in version 6.23.1 but now every method with this middleware returns the error:
LOG 4 Mar 16:45:35 [ERROR] [TSED] - {
method: 'GET',
url: '/apps/api/users/current/applications',
headers: {
host: 'localhost:3000',
connection: 'keep-alive',
pragma: 'no-cache',
'cache-control': 'no-cache',
'content-type': 'application/json; charset=utf-8',
accept: 'application/json',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
referer: 'http://localhost:3000/apps/editor/621210/KPI',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en,ru;q=0.9,en-US;q=0.8',
},
body: {},
query: {},
params: {},
reqId: '27d22fb22d864fdaa4f7be15bbb962dd',
time: 2022-03-04T13:45:35.280Z,
duration: 233,
error: {
name: 'NOT_FOUND',
message: 'Resource "/apps/api/users/current/applications" not found',
status: 404,
errors: [],
stack: 'NOT_FOUND: Resource "/apps/api/users/current/applications" not found\n' +
' at PlatformExceptions.resourceNotFound (C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\platform-exceptions\\src\\services\\PlatformExceptions.ts:51:23)\n' +
' at C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\platform-express\\src\\components\\PlatformExpress.ts:98:81\n' +
' at Layer.handle [as handle_request] (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\layer.js:95:5)\n' +
' at trim_prefix (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:323:13)\n' +
' at C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:284:7\n' +
' at Function.process_params (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:341:12)\n' +
' at next (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:275:10)\n' +
' at PlatformExpressHandler.next (C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\common\\src\\services\\PlatformHandler.ts:261:47)\n' +
' at PlatformExpressHandler.onSuccess (C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\common\\src\\services\\PlatformHandler.ts:216:17)\n' +
' at C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\common\\src\\services\\PlatformHandler.ts:156:29'
},
stack: 'NOT_FOUND: Resource "/apps/api/users/current/applications" not found\n' +
' at PlatformExceptions.resourceNotFound (C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\platform-exceptions\\src\\services\\PlatformExceptions.ts:51:23)\n' +
' at C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\platform-express\\src\\components\\PlatformExpress.ts:98:81\n' +
' at Layer.handle [as handle_request] (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\layer.js:95:5)\n' +
' at trim_prefix (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:323:13)\n' +
' at C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:284:7\n' +
' at Function.process_params (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:341:12)\n' +
' at next (C:\\dev\\confirmit.appstudio.client\\node_modules\\express\\lib\\router\\index.js:275:10)\n' +
' at PlatformExpressHandler.next (C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\common\\src\\services\\PlatformHandler.ts:261:47)\n' +
' at PlatformExpressHandler.onSuccess (C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\common\\src\\services\\PlatformHandler.ts:216:17)\n' +
' at C:\\dev\\confirmit.appstudio.client\\node_modules\\@tsed\\common\\src\\services\\PlatformHandler.ts:156:29'
}
Example
/* UsersController .ts */
import {Req, Res, UseAfter} from '@tsed/common';
import {Controller} from '@tsed/di';
import {PathParams} from '@tsed/platform-params';
import {Get, Returns} from '@tsed/schema';
import {ForbiddenNotFoundErrorMiddleware} from '../../middlewares/error-handling/ForbiddenNotFoundErrorMiddleware';
import {CmlStorageService, restClientProvider} from '../../studio-utils';
@Controller('/users')
@UseAfter(ForbiddenNotFoundErrorMiddleware)
export class UsersController {
@Get('/current/applications')
@(Returns(200, Object).ContentType('application/json'))
async getApplications(@Req() req: Req, @Res() res: Res): Promise<any> {
const restClient = await restClientProvider.getRestClient(req, res);
const applicationsResponse = await restClient.get(req.apis.applications);
const {items: applications} = applicationsResponse.json();
return applications;
}
}
/* ForbiddenNotFoundErrorMiddleware.ts */
import {FetchError, isRestClientFetchError} from '@confirmit/rest-client';
import {Err, Next, Res} from '@tsed/common';
import {Middleware, MiddlewareMethods} from '@tsed/platform-middlewares';
@Middleware()
export class ForbiddenNotFoundErrorMiddleware implements MiddlewareMethods {
use(@Err() err: unknown, @Res() res: Res, @Next() next: Next) {
if (isRestClientFetchError(err)) {
const fetchError = err as FetchError;
if ([403, 404].includes(fetchError.status)) {
res.sendStatus(404).end();
return;
}
}
next(err);
}
}
Acceptance criteria
- It should be possible to attach error middleware to certain controllers/endpoints
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 26
Commits related to this issue
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
- fix(common): fix UseAfter error middleware Closes: #1793 BREAKING CHANGE: remove support of multiple endpoint that cause unexpected behavior — committed to tsedio/tsed by Romakita 2 years ago
@zenkdev I fixed the issue with the interceptor 😉 thank for the repo Also for Middleware error, the repo help me, because it’s a very specific usecase. I haven’t fixed this issue, but I see where is the problem thanks 😉
Ok. I’ll try to repro. This is the only solution that I can propose you and I have not enought time to work on it right know.
The issue is added on the board.