Add Unsupported Media Type Exception (#11096)

* add unsuported media type exception

* throw error when not multipart/form-data

* use unsupportedMediaType when checking import file

* add `UNSUPPORTED_MEDIA_TYPE` to Error Codes docs
This commit is contained in:
Azri Kahar
2022-01-17 23:09:26 +08:00
committed by GitHub
parent 3d4602493c
commit 74bb4a7c9a
6 changed files with 36 additions and 18 deletions

View File

@@ -4,7 +4,7 @@ import express from 'express';
import Joi from 'joi';
import path from 'path';
import env from '../env';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import { ForbiddenException, InvalidPayloadException, UnsupportedMediaTypeException } from '../exceptions';
import { respond } from '../middleware/respond';
import useCollection from '../middleware/use-collection';
import { validateBatch } from '../middleware/validate-batch';
@@ -18,7 +18,8 @@ const router = express.Router();
router.use(useCollection('directus_files'));
const multipartHandler = asyncHandler(async (req, res, next) => {
if (req.is('multipart/form-data') === false) return next();
if (req.is('multipart/form-data') === false)
throw new UnsupportedMediaTypeException(`Unsupported Content-Type header`);
let headers: BusboyHeaders;

View File

@@ -2,7 +2,12 @@ import argon2 from 'argon2';
import { Router } from 'express';
import Joi from 'joi';
import { nanoid } from 'nanoid';
import { ForbiddenException, InvalidPayloadException, InvalidQueryException } from '../exceptions';
import {
ForbiddenException,
InvalidPayloadException,
InvalidQueryException,
UnsupportedMediaTypeException,
} from '../exceptions';
import collectionExists from '../middleware/collection-exists';
import { respond } from '../middleware/respond';
import { RevisionsService, UtilsService, ImportService } from '../services';
@@ -94,6 +99,9 @@ router.post(
'/import/:collection',
collectionExists,
asyncHandler(async (req, res, next) => {
if (req.is('multipart/form-data') === false)
throw new UnsupportedMediaTypeException(`Unsupported Content-Type header`);
const service = new ImportService({
accountability: req.accountability,
schema: req.schema,

View File

@@ -14,5 +14,6 @@ export * from './range-not-satisfiable';
export * from './route-not-found';
export * from './service-unavailable';
export * from './unprocessable-entity';
export * from './unsupported-media-type';
export * from './user-suspended';
export * from './unexpected-response';

View File

@@ -0,0 +1,7 @@
import { BaseException } from '@directus/shared/exceptions';
export class UnsupportedMediaTypeException extends BaseException {
constructor(message: string, extensions?: Record<string, unknown>) {
super(message, 415, 'UNSUPPORTED_MEDIA_TYPE', extensions);
}
}

View File

@@ -2,7 +2,7 @@ import { Knex } from 'knex';
import getDatabase from '../database';
import { AbstractServiceOptions, SchemaOverview } from '../types';
import { Accountability } from '@directus/shared/types';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import { ForbiddenException, InvalidPayloadException, UnsupportedMediaTypeException } from '../exceptions';
import StreamArray from 'stream-json/streamers/StreamArray';
import { ItemsService } from './items';
import { queue } from 'async';
@@ -42,7 +42,7 @@ export class ImportService {
case 'text/csv':
return await this.importCSV(collection, stream);
default:
throw new InvalidPayloadException(`Can't import files of type "${mimetype}"`);
throw new UnsupportedMediaTypeException(`Can't import files of type "${mimetype}"`);
}
}