Skip to content

Commit

Permalink
fix(files): return status code 409 on creation when it already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
sg-gs committed Sep 25, 2023
1 parent f07f499 commit 3fc73c0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/app/routes/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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}`,

Check failure on line 96 in src/app/routes/storage.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

This line has a length of 145. Maximum allowed is 120
);
Expand Down
8 changes: 8 additions & 0 deletions src/app/services/errors/FileWithNameAlreadyExistsError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/app/services/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*$/;
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 3fc73c0

Please sign in to comment.