fix error handling on upload plugin

This commit is contained in:
Pierre Noël 2021-10-26 16:41:45 +02:00
parent b58274aecb
commit 1b18aa971c
6 changed files with 17 additions and 3 deletions

View File

@ -21,7 +21,7 @@ module.exports = config => {
}
try {
return body({ patchKoa: true, ...bodyConfig })(ctx, next);
await body({ patchKoa: true, ...bodyConfig })(ctx, next);
} catch (e) {
if ((e || {}).message && e.message.includes('maxFileSize exceeded')) {
return ctx.payloadTooLarge('FileTooBig');

View File

@ -22,6 +22,8 @@ module.exports = async ({ strapi }) => {
}
await registerPermissionActions();
strapi.service('plugin::upload.errors').registerErrorMiddleware();
};
const createProvider = config => {

View File

@ -7,7 +7,7 @@ module.exports = () => {
try {
await next();
} catch (e) {
if (e instanceof FileTooLargeError) return ctx.PayloadTooLarge();
if (e instanceof FileTooLargeError) return ctx.payloadTooLarge();
if (e instanceof FileNotFoundError) return ctx.notFound();
throw e;
}

View File

@ -1,5 +1,5 @@
'use strict';
module.exports = {
'handle-errors': require('./handle-errors'),
handleErrors: require('./errors'),
};

View File

@ -0,0 +1,10 @@
'use strict';
const initErrorMiddleware = require('../middlewares/errors');
module.exports = ({ strapi }) => ({
registerErrorMiddleware() {
const errorMiddleware = initErrorMiddleware({ strapi });
strapi.server.router.use('/upload', errorMiddleware);
},
});

View File

@ -1,9 +1,11 @@
'use strict';
const errorService = require('./errors');
const uploadService = require('./upload');
const imageManipulation = require('./image-manipulation');
module.exports = {
errors: errorService,
upload: uploadService,
'image-manipulation': imageManipulation,
};