Skip to content

Commit ce0f598

Browse files
committed
feat(error handler): pass entire context nuxt object instead of only the error nuxt handler
BREAKING CHANGE: errorHandler will only have one parameter (of type Error) with the err.ctx object monkey-patched
1 parent 7eb5e33 commit ce0f598

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

lib/plugins/api.template.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ function generateAPI(controllerMapping, ctx) {
7575
ctx
7676
)
7777
.then(runSuccessHandler)
78-
.catch(runErrorHandler);
78+
.catch(err => {
79+
err.ctx = ctx;
80+
81+
return runErrorHandler(err);
82+
});
7983
}
8084
} else if (typeof controllerMapping[key] === 'object') {
8185
api[key] = generateAPI(controllerMapping[key], ctx);
@@ -86,8 +90,8 @@ function generateAPI(controllerMapping, ctx) {
8690
}
8791

8892
export default (ctx) => {
89-
const {app, req, error} = ctx;
90-
const $api = process.server ? req._controllersTree(error) : generateAPI(<%= options.controllers %>, ctx);
93+
const {app, req} = ctx;
94+
const $api = process.server ? req._controllersTree(ctx) : generateAPI(<%= options.controllers %>, ctx);
9195
app.$api = $api;
9296
Vue.prototype.$api = $api;
9397
};

lib/utility/controllers.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ function controllerMappingClientSide(directory) {
152152
* @param directory
153153
* @param req
154154
* @param options
155-
* @param nuxtError
155+
* @param ctx
156156
* @returns {Object}
157157
*/
158-
function controllerMappingServerSide(directory, req, options, nuxtError) {
158+
function controllerMappingServerSide(directory, req, options, ctx) {
159159
const apiMiddleware = options.middleware;
160160
let SuccessHandler = options.successHandler
161161
? require(options.successHandler.replace(options.aliasKey, options.srcDir))
@@ -197,7 +197,8 @@ function controllerMappingServerSide(directory, req, options, nuxtError) {
197197
return SuccessHandler && SuccessHandler(result) || Promise.resolve(result);
198198
})
199199
.catch(function (err) {
200-
return ErrorHandler && ErrorHandler(err, nuxtError) || Promise.reject(err);
200+
err.ctx = ctx;
201+
return ErrorHandler && ErrorHandler(err) || Promise.reject(err);
201202
});
202203
}
203204
});

tests/fixtures/error_handler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export default function (err, nuxtError) {
1+
export default function (err) {
22
if (err.message === "nuxtError") {
3-
return nuxtError({statusCode: 500, message: err.message});
3+
return err.ctx.error({statusCode: 500, message: err.message});
44
}
55

66
throw err;

0 commit comments

Comments
 (0)