@adonisjs/framework
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -0,1 +1,11 @@ | ||
<a name="5.0.2"></a> | ||
## [5.0.2](https://github.com/adonisjs/adonis-framework/compare/v5.0.1...v5.0.2) (2018-02-08) | ||
### Bug Fixes | ||
* **server:** bindException handler on listen ([3083662](https://github.com/adonisjs/adonis-framework/commit/3083662)) | ||
<a name="5.0.1"></a> | ||
@@ -2,0 +12,0 @@ ## [5.0.1](https://github.com/adonisjs/adonis-framework/compare/v5.0.0...v5.0.1) (2018-02-07) |
{ | ||
"name": "@adonisjs/framework", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "Adonis framework makes it easy for you to write webapps with less code", | ||
@@ -29,3 +29,3 @@ "main": "index.js", | ||
"@adonisjs/fold": "^4.0.5", | ||
"@adonisjs/sink": "^1.0.13", | ||
"@adonisjs/sink": "^1.0.14", | ||
"clear-require": "^2.0.0", | ||
@@ -64,3 +64,3 @@ "cookie-signature": "^1.1.0", | ||
"resetable": "^1.0.3", | ||
"serve-static": "^1.13.1", | ||
"serve-static": "^1.13.2", | ||
"useragent": "^2.3.0", | ||
@@ -67,0 +67,0 @@ "winston": "^2.4.0", |
@@ -146,5 +146,6 @@ 'use strict' | ||
const Exception = app.use('Adonis/Src/Exception') | ||
const Helpers = app.use('Adonis/Src/Helpers') | ||
const Logger = app.use('Adonis/Src/Logger') | ||
const Server = require('../src/Server') | ||
return new Server(Context, Route, Logger, Exception) | ||
return new Server(Context, Route, Logger, Exception, Helpers) | ||
}) | ||
@@ -151,0 +152,0 @@ this.app.alias('Adonis/Src/Server', 'Server') |
@@ -15,6 +15,8 @@ 'use strict' | ||
const _ = require('lodash') | ||
const { resolver } = require('@adonisjs/fold') | ||
const { resolver, ioc } = require('@adonisjs/fold') | ||
const path = require('path') | ||
const debug = require('debug')('adonis:framework') | ||
const GE = require('@adonisjs/generic-exceptions') | ||
const Middleware = require('co-compose') | ||
const fs = require('fs') | ||
@@ -76,3 +78,3 @@ /** | ||
class Server { | ||
constructor (Context, Route, Logger, Exception) { | ||
constructor (Context, Route, Logger, Exception, Helpers) { | ||
this.Context = Context | ||
@@ -82,4 +84,6 @@ this.Route = Route | ||
this.Exception = Exception | ||
this.Helpers = Helpers | ||
this._httpInstance = null | ||
this._exceptionHandlerNamespace = null | ||
@@ -94,8 +98,2 @@ /** | ||
} | ||
/** | ||
* Exception handler class to respond | ||
* to an exception. | ||
*/ | ||
this._ExceptionHandler = null | ||
} | ||
@@ -124,2 +122,24 @@ | ||
/** | ||
* Returns the exception handler to handle the HTTP exceptions | ||
* | ||
* @method _getExceptionHandlerNamespace | ||
* | ||
* @return {Class} | ||
* | ||
* @private | ||
*/ | ||
_getExceptionHandlerNamespace () { | ||
const appRoot = this.Helpers.appRoot() | ||
const exceptionsDir = this.Helpers.directories['exceptions'] | ||
const exceptionHandlerFile = path.join(appRoot, exceptionsDir, 'Handler.js') | ||
try { | ||
fs.accessSync(exceptionHandlerFile, fs.constants.R_OK) | ||
return resolver.forDir('exceptions').translate('Handler') | ||
} catch (error) { | ||
return 'Adonis/Exceptions/BaseExceptionHandler' | ||
} | ||
} | ||
/** | ||
* Takes middleware as a function or string and returns an object, that | ||
@@ -437,3 +457,10 @@ * can be used at runtime to resolve and run middleware | ||
try { | ||
const handler = new this._ExceptionHandler() | ||
const handler = ioc.make(ioc.use(this._exceptionHandlerNamespace)) | ||
if (typeof (handler.handle) !== 'function' || typeof (handler.report) !== 'function') { | ||
throw GE | ||
.RuntimeException | ||
.invoke(`${this._exceptionHandlerNamespace} class must have handle and report methods on it`) | ||
} | ||
handler.report(error, { request: ctx.request, auth: ctx.auth }) | ||
@@ -593,18 +620,2 @@ await handler.handle(error, ctx) | ||
/** | ||
* Sets the exception handler to be used for handling exceptions | ||
* | ||
* @method setExceptionHandler | ||
* | ||
* @param {Class} handler | ||
* | ||
* @return {void} | ||
*/ | ||
setExceptionHandler (handler) { | ||
if (typeof (handler.prototype.handle) !== 'function' || typeof (handler.prototype.report) !== 'function') { | ||
throw GE.RuntimeException.invoke('Http exception handler must have handle and report methods on it') | ||
} | ||
this._ExceptionHandler = handler | ||
} | ||
/** | ||
* Handle method executed for each HTTP request and handles | ||
@@ -667,2 +678,19 @@ * the request lifecycle by performing following operations. | ||
/** | ||
* Binds the exception handler to be used for handling HTTP | ||
* exceptions. If `namespace` is not provided, the server | ||
* will choose the conventional namespace | ||
* | ||
* @method bindExceptionHandler | ||
* | ||
* @param {String} [namespace] | ||
* | ||
* @chainable | ||
*/ | ||
bindExceptionHandler (namespace) { | ||
this._exceptionHandlerNamespace = namespace || this._getExceptionHandlerNamespace() | ||
debug('using %s binding to handle exceptions', this._exceptionHandlerNamespace) | ||
return this | ||
} | ||
/** | ||
* Listen on given host and port. | ||
@@ -679,2 +707,6 @@ * | ||
listen (host = 'localhost', port = 3333, callback) { | ||
if (!this._exceptionHandlerNamespace) { | ||
this.bindExceptionHandler() | ||
} | ||
this.Logger.info('serving app on http://%s:%s', host, port) | ||
@@ -681,0 +713,0 @@ return this.getInstance().listen(port, host, callback) |
196897
6279
14
Updatedserve-static@^1.13.2