adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.76k stars 637 forks source link

Error Pages not working (Handler.ts) #3414

Closed claudesortwell closed 2 years ago

claudesortwell commented 2 years ago

What is happening?

Adonis is not rendering error pages defined in statusPages in handler.ts, this also does not working in dev using the optional boolean. A prime example is the Adonis Poll example, but even in my private repo, I have the same problem.

Package version

 "devDependencies": {
    "@adonisjs/assembler": "^5.3.0",
    "@symfony/webpack-encore": "^1.7.0",
    "adonis-preset-ts": "^2.1.0",
  },
  "dependencies": {
    "@adonisjs/core": "^5.4.0",
    "@adonisjs/drive-s3": "^1.0.5",
    "@adonisjs/mail": "^7.2.4",
    "@adonisjs/repl": "^3.1.0",
    "@adonisjs/session": "^6.1.2",
    "@adonisjs/view": "^6.1.1",
}

Node.js and npm version

v14.16.1, Yarn 3.1.1

Sample Code (to reproduce the issue)

  protected statusPages = {
    '403': 'pages/errors/unauthorized',
    '404': 'pages/errors/not-found',
    '500..599': 'pages/errors/server-error',
  }

BONUS (a sample repo to reproduce the issue)

Polls App Example route not founds not working https://polls.adonisjs.dev/qwfqwfeqf. According to the repos handler.ts, it should render the route not found edge page but the link is rendering the default error:

E_ROUTE_NOT_FOUND: Cannot GET:/qwfqwfeqf
thetutlage commented 2 years ago

There was an issue with the polls app in which the exception handler itself was raising exception. Hence the server returned a generic error. I have fixed that error here https://github.com/adonisjs-community/polls-app/commit/553f25d6decfe88cf72ada4af33c58b48e41fc80 and now the poll app does render status pages.

Are you testing the same on local? If yes, then status pages are only rendered when the NODE_ENV=production. However, there is a flag to force status page to render in development too. https://docs.adonisjs.com/guides/exception-handling#status-pages

claudesortwell commented 2 years ago

I'll provide a sample repo tomorrow for you. Ive tried adding the force status flag, no luck. I think it might have to do with starting adonis with the api template then adding front end.

thetutlage commented 2 years ago

Ohh is it that you are using the API template? If yes, then shouldn't you expect your API server to always return JSON and not HTML?

claudesortwell commented 2 years ago

Yeah ive used the API Template then added Views. So im guessing there is no way to make it return html errors after adding views. I've added a catch all route for 404, in the view side

thetutlage commented 2 years ago

Yes, as of now the status pages are not for the API template. However, I am still curious to know why you want to return HTML from an API server?

claudesortwell commented 2 years ago

Yeah makes sense. It's kinda of a hybrid application. So API for customer's SPA then we have an admin portal built with the adonis views.

You can probably close this now, as I understand your reasoning for not adding views in an API template. Maybe adding some middleware error catching or a custom handler will work for hybrid applications.

thetutlage commented 2 years ago

You can still manage it, if your API is nested within some prefix like /api. Then you can use the changes implemented in this PR https://github.com/adonisjs/http-server/pull/51/files#diff-02b5b8ba60024cffe8cebc1ff41db79cd8baab6e1fb61101ffbcd2eff3e04ff3R591

Lemme know if that is the case and you need any help with implementation