You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why is it that when I use the same ctx to call the injected t (i18n) in the wrong handler, my E_ROW_NOT_FOUND can be caught normally and return i18n information, but E_ROUTE_NOT_FOUND throws an error: Cannot read properties of undefined (reading 't')?
importappfrom'@adonisjs/core/services/app'import{HttpContext,ExceptionHandler}from'@adonisjs/core/http'importtype{StatusPageRange,StatusPageRenderer}from'@adonisjs/core/types/http'import{errors}from'@adonisjs/core'import{errorsasmodelErrors}from'@adonisjs/lucid'import{errorsasvalidatorErros}from'@vinejs/vine'importApiResourcefrom'#resources/api_resource'exportdefaultclassHttpExceptionHandlerextendsExceptionHandler{/** * In debug mode, the exception handler will display verbose errors * with pretty printed stack traces. */protecteddebug=!app.inProduction/** * Status pages are used to display a custom HTML pages for certain error * codes. You might want to enable them in production only, but feel * free to enable them in development as well. */protectedrenderStatusPages=app.inProduction/** * Status pages is a collection of error code range and a callback * to return the HTML contents to send as a response. */protectedstatusPages: Record<StatusPageRange,StatusPageRenderer>={'404': (error,{ inertia })=>inertia.render('errors/not_found',{ error }),'500..599': (error,{ inertia })=>inertia.render('errors/server_error',{ error }),}/** * The method is used for handling errors and returning * response to the client */asynchandle(error: unknown,ctx: HttpContext){// 404 路由if(errorinstanceoferrors.E_ROUTE_NOT_FOUND){ctx.response.status(404).send(ApiResource.error(404,ctx.i18n.t(`errors.routeNotFound`)))return}// 404 模型if(errorinstanceofmodelErrors.E_ROW_NOT_FOUND){ctx.response.status(404).send(ApiResource.error(404,ctx.i18n.t(`errors.modelNotFound`)))return}// 422 验证if(errorinstanceofvalidatorErros.E_VALIDATION_ERROR){ctx.response.status(422).send(ApiResource.error(422,ctx.i18n.t(`errors.validationFailure`),error.messages))return}// if (error instanceof errors.E_HTTP_EXCEPTION) {// ctx.response.status(500).send(ApiResource.error(500, error.message, error.stack))// }returnsuper.handle(error,ctx)}/** * The method is used to report error to the logging service or * the a third party error monitoring service. * * @note You should not attempt to send a response from this method. */asyncreport(error: unknown,ctx: HttpContext){returnsuper.report(error,ctx)}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Why is it that when I use the same ctx to call the injected t (i18n) in the wrong handler, my E_ROW_NOT_FOUND can be caught normally and return i18n information, but E_ROUTE_NOT_FOUND throws an error: Cannot read properties of undefined (reading 't')?
Beta Was this translation helpful? Give feedback.
All reactions