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.33k stars 624 forks source link

HttpExceptionHandler for makeHtmlResponse is not rendering error view #2694

Closed daltonvarao closed 3 years ago

daltonvarao commented 3 years ago

While I was setting up a page for status 404 for my project, I noticed that the page was not rendered when a non registered route is accessed even in a production environment or with disableStatusPagesInDevelopment = false setted on ExceptionHandler. So I decide to look for examples of same issues. Here are some issues that i founded:

But none of them has the real solution.

When looking in HttpExceptionHandler code i found the possible of the error

if (ctx['view'] && this.expandedStatusPages[error.status]) {     
  const html = ctx['view'].render(this.expandedStatusPages[error.status], { error })      
  ctx.response.status(error.status).send(html)      
  return ; 
}

The method view.render() above returns a promise, so the await operator is missing. So, when inserting the operator the page was rendered correctly.

I assume that this error was not detected in the test for the following cause: in the test only the statusCode validation of the response is done and if the view name matches what was set in statusPages, however the return of the view.render call is not validated .

Package version


{
  "name": "projel-produtividade",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "node ace build --production",
    "start": "node server.js",
    "dev": "node ace serve --watch",
    "lint": "eslint . --ext=.ts",
    "format": "prettier --write .",
    "test": "node -r @adonisjs/assembler/build/register japaFile.ts"
  },
  "devDependencies": {
    "@adonisjs/assembler": "^5.0.0",
    "@babel/preset-react": "^7.13.13",
    "@babel/preset-typescript": "^7.0.0",
    "@symfony/webpack-encore": "^1.2.0",
    "@types/react": "^17.0.5",
    "@types/react-dom": "^17.0.3",
    "@types/react-icons": "^3.0.0",
    "@types/styled-components": "^5.1.9",
    "@types/supertest": "^2.0.11",
    "adonis-preset-ts": "^2.1.0",
    "axios": "^0.21.1",
    "eslint": "^7.26.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-adonis": "^1.3.1",
    "eslint-plugin-prettier": "^3.4.0",
    "japa": "^3.1.1",
    "pino-pretty": "^4.7.1",
    "prettier": "^2.2.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-icons": "^4.2.0",
    "styled-components": "^5.3.0",
    "supertest": "^6.1.3",
    "typescript": "~4.2",
    "youch": "^2.2.2",
    "youch-terminal": "^1.1.1"
  },
  "dependencies": {
    "@adonisjs/auth": "^8.0.3",
    "@adonisjs/core": "^5.1.0",
    "@adonisjs/lucid": "^14.0.1",
    "@adonisjs/repl": "^3.0.0",
    "@adonisjs/session": "^6.0.0",
    "@adonisjs/shield": "^7.0.0",
    "@adonisjs/view": "^6.0.0",
    "luxon": "^1.26.0",
    "pg": "^8.6.0",
    "phc-argon2": "^1.1.1",
    "proxy-addr": "^2.0.6",
    "reflect-metadata": "^0.1.13",
    "source-map-support": "^0.5.19"
  }
}

Node.js and npm version

Node.js v14.15.0 and npm v6.14.8

Sample Code (to reproduce the issue)

$ yarn create adonis-ts-app app-name

Are you willing to work on it with little guidance?

Yes, i can submit a PR for the correction.

thetutlage commented 3 years ago

Fixed in https://github.com/adonisjs/core/pull/2697

thetutlage commented 3 years ago

Sorry, it took a while, as there were other things that has to make to the release. It is out now as 5.1.8