Skip to content

Commit

Permalink
allow serializer methods to return a promise
Browse files Browse the repository at this point in the history
  • Loading branch information
gr0uch committed Sep 1, 2015
1 parent 4ce3952 commit 8a6c406
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
4 changes: 1 addition & 3 deletions lib/dispatch/end.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export default function (context) {
if (records) args.push(records)
if (include) args.push(include)

context = serializer.showResponse(...args)

return context
return serializer.showResponse(...args)
})
}
12 changes: 6 additions & 6 deletions lib/dispatch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ export default function dispatch (options, ...args) {
return new OK(response)
})

.catch(error => {
context = showError(context, nativeErrors.has(error.constructor) ?
new Error(`An internal server error occurred.`) : error)
.catch(error => Promise.resolve(
showError(context, nativeErrors.has(error.constructor) ?
new Error(`An internal server error occurred.`) : error))

.then(context => Promise.resolve(processResponse(context, ...args)))

return Promise.resolve(processResponse(context, ...args))
.then(context => {
throw Object.assign(error, context.response)
})
})
}))
}


Expand Down
4 changes: 2 additions & 2 deletions lib/serializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class Serializer {
* @param {Object} context
* @param {Object[]} [records]
* @param {Object} [include]
* @return {Object}
* @return {Promise|Object}
*/
showResponse (context) {
return context
Expand All @@ -100,7 +100,7 @@ export default class Serializer {
*
* @param {Object} context
* @param {Object} error should be an instance of Error
* @return {Object}
* @return {Promise|Object}
*/
showError (context) {
return context
Expand Down
14 changes: 4 additions & 10 deletions lib/serializer/singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ export default class SerializerSingleton extends Serializer {


const inputMethods = new Set([ 'parseCreate', 'parseUpdate' ])
const asynchronousMethods = new Set([
'processRequest', 'processResponse',
'parseCreate', 'parseUpdate'
])


// Assign the proxy methods on top of the base methods.
Expand Down Expand Up @@ -124,19 +120,17 @@ function proxyMethod (options, context, ...args) {
// Fail if no serializer was found.
else throw new NoopError(`The serializer for "${format}" is unrecognized.`)

const isAsynchronous = asynchronousMethods.has(method)

try {
const result = serializer[method](context, ...args)
return isAsynchronous ? Promise.resolve(result) : result
return Promise.resolve(serializer[method](context, ...args))
}
catch (e) {
let error = e

// Only in the special case of input methods, it may be more appropriate to
// throw a BadRequestError.
if (nativeErrors.has(error.constructor) && isInput)
error = new BadRequestError(`The request is malformed.`)

if (isAsynchronous) return Promise.reject(error)
throw error
return Promise.reject(error)
}
}

0 comments on commit 8a6c406

Please sign in to comment.