diff --git a/src/app/routes/storage.ts b/src/app/routes/storage.ts index 4fbaac37..7b452920 100644 --- a/src/app/routes/storage.ts +++ b/src/app/routes/storage.ts @@ -12,7 +12,10 @@ import { FileAttributes } from '../models/file'; import CONSTANTS from '../constants'; import { LockNotAvaliableError } from '../services/errors/locks'; import { ConnectionTimedOutError } from 'sequelize'; -import { FileWithNameAlreadyExistsError } from '../services/errors/FileWithNameAlreadyExistsError'; +import { + FileAlreadyExistsError, + FileWithNameAlreadyExistsError +} from '../services/errors/FileWithNameAlreadyExistsError'; import { FolderWithNameAlreadyExistsError } from '../services/errors/FolderWithNameAlreadyExistsError'; import * as resourceSharingMiddlewareBuilder from '../middleware/resource-sharing.middleware'; @@ -86,6 +89,9 @@ export class StorageController { }), ); } catch (err) { + if (err instanceof FileAlreadyExistsError) { + return res.status(409).send({ error: err.message }); + } this.logger.error( `[FILE/CREATE] ERROR: ${(err as Error).message}, BODY ${JSON.stringify(file)}, STACK: ${(err as Error).stack} USER: ${behalfUser.email}`, ); diff --git a/src/app/services/errors/FileWithNameAlreadyExistsError.ts b/src/app/services/errors/FileWithNameAlreadyExistsError.ts index c4d8d1b5..1f3a44e1 100644 --- a/src/app/services/errors/FileWithNameAlreadyExistsError.ts +++ b/src/app/services/errors/FileWithNameAlreadyExistsError.ts @@ -5,3 +5,11 @@ export class FileWithNameAlreadyExistsError extends Error { Object.setPrototypeOf(this, FileWithNameAlreadyExistsError.prototype); } } + +export class FileAlreadyExistsError extends Error { + constructor(message: string) { + super(message); + + Object.setPrototypeOf(this, FileAlreadyExistsError.prototype); + } +} diff --git a/src/app/services/files.js b/src/app/services/files.js index 9ab168cc..1b3311d9 100644 --- a/src/app/services/files.js +++ b/src/app/services/files.js @@ -3,7 +3,7 @@ const async = require('async'); const createHttpError = require('http-errors'); const AesUtil = require('../../lib/AesUtil'); const { v4 } = require('uuid'); -const { FileWithNameAlreadyExistsError } = require('./errors/FileWithNameAlreadyExistsError'); +const { FileWithNameAlreadyExistsError, FileAlreadyExistsError } = require('./errors/FileWithNameAlreadyExistsError'); // Filenames that contain "/", "\" or only spaces are invalid const invalidName = /[/\\]|^\s*$/; @@ -43,7 +43,7 @@ module.exports = (Model, App) => { const fileAlreadyExists = !!maybeAlreadyExistentFile; if (fileAlreadyExists) { - throw new Error('File already exists'); + throw new FileAlreadyExistsError('File already exists'); } const fileInfo = {