Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): option to customize error responses #611

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/express-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ const uploads = uploadx({
}
});

app.use('/uploads', uploads);
app.use('/files', uploads);

app.listen(PORT, () => console.log('listening on port:', PORT));
10 changes: 9 additions & 1 deletion examples/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ app.use(
directory: 'upload',
expiration: { maxAge: '1h', purgeInterval: '10min' },
userIdentifier: (req: AuthRequest) => (req.user ? `${req.user.id}-${req.user.email}` : ''),
logger
logger,
onError: ({ statusCode, body }) => {
const errors = [{ status: statusCode, title: body?.code, detail: body?.message }];
return {
statusCode,
headers: { 'Content-Type': 'application/vnd.api+json' },
body: { errors }
};
}
}),
onComplete
);
Expand Down
19 changes: 7 additions & 12 deletions packages/core/src/handlers/base-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
fail,
getBaseUrl,
hash,
HttpErrorBody,
IncomingMessageWithBody,
isUploadxError,
isValidationError,
Expand Down Expand Up @@ -207,7 +208,7 @@ export abstract class BaseHandler<TFile extends UploadxFile>
let data: string;
if (typeof body !== 'string') {
data = JSON.stringify(body);
res.setHeader('Content-Type', 'application/json');
if (!headers['Content-Type']) res.setHeader('Content-Type', 'application/json');
} else {
data = body;
}
Expand All @@ -221,21 +222,15 @@ export abstract class BaseHandler<TFile extends UploadxFile>
* Send Error to client
*/
sendError(res: http.ServerResponse, error: Error): void {
const response = isUploadxError(error)
const httpError = isUploadxError(error)
? this._errorResponses[error.uploadxErrorCode]
: !isValidationError(error)
? this.storage.normalizeError(error)
: error;
const { statusCode = 200, headers, ...rest } = response;
const body = response.body ? response.body : rest;
this.send(res, this.formatErrorResponse({ statusCode, body, headers }));
}

/**
* Adjusting the error response
*/
formatErrorResponse({ statusCode, body, headers }: UploadxResponse): UploadxResponse {
return { statusCode, body: { error: body }, headers };
const { statusCode = 200, headers, ...rest } = httpError;
const body = httpError.body ? httpError.body : (rest as HttpErrorBody);
const response = { statusCode, body, headers };
this.send(res, this.storage.onError(response) || response);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/storages/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { logger, Logger } from '../utils';
import { HttpError, logger } from '../utils';
import { File } from './file';
import { BaseStorageOptions } from './storage';

class ConfigHandler {
export class ConfigHandler {
static defaults = {
allowMIME: ['*/*'],
maxUploadSize: '5TB',
filename: ({ id }: File): string => id,
useRelativeLocation: false,
onComplete: () => null,
onError: ({ statusCode, body, headers }: HttpError) => {
return { statusCode, body: { error: body }, headers };
},
path: '/files',
validation: {},
maxMetadataSize: '4MB',
Expand All @@ -24,10 +27,4 @@ class ConfigHandler {
get<T extends File>(): Required<BaseStorageOptions<T>> {
return this._config as unknown as Required<BaseStorageOptions<T>>;
}

getLogger(): Logger {
return this._config.logger;
}
}

export const configHandler = new ConfigHandler();
2 changes: 1 addition & 1 deletion packages/core/src/storages/disk-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DiskStorage extends BaseStorage<DiskFile> {
if (config.metaStorage) {
this.meta = config.metaStorage;
} else {
const metaConfig = { ...config, ...(config.metaStorageConfig || {}) };
const metaConfig = { ...config, ...(config.metaStorageConfig || {}), logger: this.logger };
this.meta = new LocalMetaStorage(metaConfig);
}
this.accessCheck().catch(err => {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/storages/meta-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FileName } from './file';
import { configHandler } from './config';
import { logger, Logger } from '../utils';

/** @experimental */
export interface UploadListEntry {
Expand All @@ -19,6 +19,7 @@ export interface UploadList {
export interface MetaStorageOptions {
prefix?: string;
suffix?: string;
logger?: Logger;
}

/**
Expand All @@ -27,13 +28,14 @@ export interface MetaStorageOptions {
export class MetaStorage<T> {
prefix = '';
suffix = '';
logger = configHandler.getLogger();
logger: Logger;

constructor(config?: MetaStorageOptions) {
this.prefix = config?.prefix || '';
this.suffix = config?.suffix || METAFILE_EXTNAME;
this.prefix && FileName.INVALID_PREFIXES.push(this.prefix);
this.suffix && FileName.INVALID_SUFFIXES.push(this.suffix);
this.logger = config?.logger || logger;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/storages/storage.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import * as bytes from 'bytes';
import * as http from 'http';

import {
Cache,
ErrorMap,
ErrorResponses,
ERRORS,
fail,
HttpError,
HttpErrorBody,
isEqual,
Locker,
Logger,
LogLevel,
toMilliseconds,
typeis,
UploadxResponse,
Validation,
Validator,
ValidatorConfig
} from '../utils';
import { File, FileInit, FileName, FilePart, FileQuery, isExpired, updateMetadata } from './file';
import { MetaStorage, UploadList } from './meta-storage';
import { setInterval } from 'timers';
import { configHandler } from './config';
import { ConfigHandler } from './config';

export type UserIdentifier = (req: any, res: any) => string;

export type OnComplete<TFile extends File, TResponseBody = any> = (
file: TFile
) => Promise<TResponseBody> | TResponseBody;

export type OnError<TResponseBody = HttpErrorBody> = (error: HttpError<TResponseBody>) => any;

export type PurgeList = UploadList & { maxAgeMs: number };

export interface ExpirationOptions {
Expand Down Expand Up @@ -59,6 +62,7 @@ export interface BaseStorageOptions<T extends File> {
useRelativeLocation?: boolean;
/** Completed callback */
onComplete?: OnComplete<T>;
onError?: OnError;
/** Node http base path */
path?: string;
/** Upload validation options */
Expand Down Expand Up @@ -97,6 +101,7 @@ export const locker = new Locker(1000, LOCK_TIMEOUT);

export abstract class BaseStorage<TFile extends File> {
onComplete: (file: TFile) => Promise<any> | any;
onError: (err: HttpError) => UploadxResponse;
maxUploadSize: number;
maxMetadataSize: number;
path: string;
Expand All @@ -110,9 +115,11 @@ export abstract class BaseStorage<TFile extends File> {
abstract meta: MetaStorage<TFile>;

protected constructor(public config: BaseStorageOptions<TFile>) {
const configHandler = new ConfigHandler();
const opts = configHandler.set(config);
this.path = opts.path;
this.onComplete = opts.onComplete;
this.onError = opts.onError;
this.namingFunction = opts.filename;
this.maxUploadSize = bytes.parse(opts.maxUploadSize);
this.maxMetadataSize = bytes.parse(opts.maxMetadataSize);
Expand Down
2 changes: 1 addition & 1 deletion packages/gcs/src/gcs-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class GCStorage extends BaseStorage<GCSFile> {
if (config.metaStorage) {
this.meta = config.metaStorage;
} else {
const metaConfig = { ...config, ...(config.metaStorageConfig || {}) };
const metaConfig = { ...config, ...(config.metaStorageConfig || {}), logger: this.logger };
this.meta =
'directory' in metaConfig
? new LocalMetaStorage(metaConfig)
Expand Down
2 changes: 1 addition & 1 deletion packages/s3/src/s3-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class S3Storage extends BaseStorage<S3File> {
if (config.metaStorage) {
this.meta = config.metaStorage;
} else {
const metaConfig = { ...config, ...(config.metaStorageConfig || {}) };
const metaConfig = { ...config, ...(config.metaStorageConfig || {}), logger: this.logger };
this.meta =
'directory' in metaConfig
? new LocalMetaStorage(metaConfig)
Expand Down
3 changes: 2 additions & 1 deletion test/shared/uploader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BaseHandler,
BaseStorage,
BaseStorageOptions,
File,
FileInit,
FilePart,
Expand All @@ -14,7 +15,7 @@ export class TestStorage extends BaseStorage<File> {
path = '/files';
isReady = true;
meta = new MetaStorage<File>();
constructor(config = {}) {
constructor(config = {} as BaseStorageOptions<File>) {
super(config);
}

Expand Down
71 changes: 48 additions & 23 deletions test/uploadx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ jest.mock('fs');
describe('::Uploadx', () => {
const file1 = { ...metadata };
const file2 = { ...metadata, name: 'testfile2.mp4' };
let uri1 = '';
let uri2 = '';
let start: number;
const path1 = '/uploadx';
const path2 = '/uploadx2';
const directory = join(testRoot, 'uploadx');
const opts = { ...storageOptions, directory, maxMetadataSize: 250 };
const uploadx2 = new Uploadx({ storage: new DiskStorage(opts) });
const uploadx2 = new Uploadx({
storage: new DiskStorage({
...storageOptions,
onError: ({ statusCode, body }) => {
const errors = [{ status: statusCode, title: body?.code, detail: body?.message }];
return {
statusCode,
headers: { 'Content-Type': 'application/vnd.api+json' },
body: { errors }
};
}
})
});
app.use(path1, uploadx(opts));
app.use(path2, uploadx2.handle);
function exposedHeaders(response: request.Response): string[] {
Expand All @@ -35,9 +44,9 @@ describe('::Uploadx', () => {
.send(file);
}

beforeAll(async () => cleanup(directory));
beforeEach(async () => cleanup(directory));

afterAll(async () => cleanup(directory));
afterEach(async () => cleanup(directory));

describe('default options', () => {
it('should be defined', () => {
Expand Down Expand Up @@ -78,9 +87,8 @@ describe('::Uploadx', () => {

it('should 201 (x-headers)', async () => {
const res = await create(file1).expect(201).expect('x-upload-expires', /.*/);
uri1 = res.header.location as string;
expect(exposedHeaders(res)).toEqual(expect.arrayContaining(['location', 'x-upload-expires']));
expect(uri1).toBeDefined();
expect(res.headers.location).toBeDefined();
});

it('should 201 (metadata)', async () => {
Expand All @@ -89,9 +97,8 @@ describe('::Uploadx', () => {
.send(file2)
.expect(201)
.expect('x-upload-expires', /.*/);
uri2 = res.header.location as string;
expect(exposedHeaders(res)).toEqual(expect.arrayContaining(['location', 'x-upload-expires']));
expect(uri2).toBeDefined();
expect(res.header.location).toBeDefined();
});

it('should 201 (query)', async () => {
Expand All @@ -106,17 +113,17 @@ describe('::Uploadx', () => {

describe('PATCH', () => {
it('update metadata', async () => {
uri2 ||= (await create(file2)).header.location;
const res = await request(app).patch(uri2).send({ name: 'newname.mp4' }).expect(200);
const uri = (await create(file2)).header.location as string;
const res = await request(app).patch(uri).send({ name: 'newname.mp4' }).expect(200);
expect(res.body.name).toBe('newname.mp4');
});
});

describe('PUT', () => {
it('should 200 (simple request)', async () => {
uri2 ||= (await create(file2)).header.location;
const uri = (await create(file2)).header.location as string;
const res = await request(app)
.put(uri2)
.put(uri)
.set('Digest', `sha=${metadata.sha1}`)
.send(testfile.asBuffer)
.expect(200);
Expand All @@ -125,17 +132,16 @@ describe('::Uploadx', () => {
});

it('should 200 (chunks)', async () => {
uri1 ||= (await create(file1)).header.location;

const uri = (await create(file1)).header.location as string;
function uploadChunks(): Promise<request.Response> {
return new Promise(resolve => {
start = 0;
let start = 0;
const readable = testfile.asReadable;
// eslint-disable-next-line @typescript-eslint/no-misused-promises
readable.on('data', async (chunk: { length: number }) => {
readable.pause();
const res = await request(app)
.put(uri1)
.put(uri)
.redirects(0)
.set('content-type', 'application/octet-stream')
.set('content-range', `bytes ${start}-${start + chunk.length - 1}/${metadata.size}`)
Expand Down Expand Up @@ -224,17 +230,17 @@ describe('::Uploadx', () => {

describe('GET', () => {
it('should return info array', async () => {
uri1 ||= (await create(file1)).header.location;
uri2 ||= (await create(file2)).header.location;
await create(file1);
await create(file2);
const res = await request(app).get(`${path1}`).expect(200);
expect(res.body.items.length).toBeGreaterThan(2);
expect(res.body.items).toHaveLength(2);
});
});

describe('DELETE', () => {
it('should 204', async () => {
uri2 ||= (await create(file2)).header.location;
await request(app).delete(uri2).expect(204);
const uri = (await create(file2)).header.location as string;
await request(app).delete(uri).expect(204);
});
});

Expand All @@ -244,6 +250,25 @@ describe('::Uploadx', () => {
});
});

describe('onError', () => {
it('should return custom eeror response', async () => {
const res = await request(app)
.post(path2)
.send({ ...file2, size: 10e10 });
expect(res.status).toBe(413);
expect(res.body).toMatchObject({
errors: [
{
detail: 'Request entity too large',
status: 413,
title: 'RequestEntityTooLarge'
}
]
});
expect(res.type).toBe('application/vnd.api+json');
});
});

describe('events', () => {
it('should emit `created`', async () => {
let event;
Expand Down
Loading