Base error handler for Ash applications. Users can choose to inherit from this class and override its error method in order to define a custom error handler for their application.
// app/error-handler.js
const Ash = require('@ash-framework/ash')
class ErrorHandler extends Ash.ErrorHandler {
error (err) {
super.error(err)
// perform custom operations
}
}
accepts
(
Mixed
-
types
Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field.
Example:
`
javascript
this.accepts(['json', 'text'])
// => "json"
`
Note: This is a proxy of the express js request.accepts
method.Parameters:
-
types
Mixed- may be a single MIME type string (such as “application/json”), an extension name such as “json”, a comma-delimited list, or an array.
Returns:
Mixed:
Returns the best match, or if none of the specified content types is acceptable,
returns false.
constructor
(
-
context
Constructs a new http object.
Sets up request and response properties and injects services if defined.
Sets the following properties on the route:
- request (express request)
- response (express response)
- body (request body)
- params (request url named parameters)
- query (request url query parameters)
- headers (request headers)
- method (request method)
Services are injected under the defined injection property.
Example:
Given the following service definition:
`
javascript
static services(register) {
register('authentication')
}
`
The route will be able to access the service:
`
javascript
this.authentication
`
Parameters:
-
context
Object- object with propertiesrequest
andresponse
which are the express js request and reponse objects respectively
error
(
public
-
err
Method called by Ash whenever an error occurs. This includes 404 errors
Parameters:
-
err
Object- error object with properties
status
andmessage
- error object with properties
is
(
Mixed
-
type
Determines if the incoming request’s “Content-Type” HTTP header field matches
the MIME type specified by the type parameter.
Note: This is a proxy of the express js
request.is
method.
Example:
`
javascript
// When Content-Type is application/json
this.is('json')
this.is('application/json')
this.is('application/')
// => true
`
Parameters:
-
type
String
Returns:
Mixed:
Returns
true
if the incoming request’s “Content-Type” HTTP header field matches
the MIME type specified by the type parameter. Returns false
otherwise.
headers
Object
The request headers object.
Contains Key-value pairs of header names and values. Header names are lower-cased.
Duplicates in raw headers are handled in the following ways, depending on the header name:
Duplicates of age, authorization, content-length, content-type, etag, expires, from, host, if-modified-since, if-unmodified-since, last-modified, location, max-forwards, proxy-authorization, referer, retry-after, or user-agent are discarded.
set-cookie is always an array. Duplicates are added to the array.
For all other headers, the values are joined together with ', '.
Example:
`
javascript
this.headers
// { 'user-agent': 'curl/7.22.0',
// host: '127.0.0.1:3010',
// accept: '*\/*' }
`
method
Object
Contains a string corresponding to the HTTP method of the
request: GET, POST, PUT, and so on.
Example:
`
javascript
this.method // GET
`
params
Object
Named url parameters
This property is an object containing properties mapped to named route parameters.
This object defaults to {}.
Example: defining parameters
`
javascript
// app/router.js
Router.map(function () {
this.route('users', {path: '/users/:user_id'})
})
`
Example: accessing defined parameters
`
javascript
this.params.user_id
`
query
Object
This property is an object containing a property for each query
string parameter in the route. If there is no query string, it is
an empty object
Example:
`
javascript
// /users?age=20&name=bob
this.query.age // 20
this.query.name // bob
`
request
Object
The http request object.
This is the express request object
See http://expressjs.com/en/api.html#req
response
Object
The http response object.
This is the express response object
See http://expressjs.com/en/api.html#res