From 4965da48abb5a13c098ae2c5b15016eff09b8b2b Mon Sep 17 00:00:00 2001 From: Wouter Termont Date: Wed, 6 Apr 2022 16:11:31 +0200 Subject: [PATCH] feat: add more redirect flavours Signed-off-by: Wouter Termont --- src/util/errors/FoundHttpError.ts | 1 + src/util/errors/MovedPermanentlyHttpError.ts | 1 + src/util/errors/PermanentRedirectHttpError.ts | 16 ++++++++++++++++ src/util/errors/SeeOtherHttpError.ts | 17 +++++++++++++++++ src/util/errors/TemporaryRedirectHttpError.ts | 16 ++++++++++++++++ test/unit/util/errors/RedirectHttpError.test.ts | 6 ++++++ 6 files changed, 57 insertions(+) create mode 100644 src/util/errors/PermanentRedirectHttpError.ts create mode 100644 src/util/errors/SeeOtherHttpError.ts create mode 100644 src/util/errors/TemporaryRedirectHttpError.ts diff --git a/src/util/errors/FoundHttpError.ts b/src/util/errors/FoundHttpError.ts index 9e33035d1..2a8e0498e 100644 --- a/src/util/errors/FoundHttpError.ts +++ b/src/util/errors/FoundHttpError.ts @@ -3,6 +3,7 @@ import { RedirectHttpError } from './RedirectHttpError'; /** * Error used for resources that have been moved temporarily. + * Methods other than GET may or may not be changed to GET in subsequent requests. */ export class FoundHttpError extends RedirectHttpError { public constructor(location: string, message?: string, options?: HttpErrorOptions) { diff --git a/src/util/errors/MovedPermanentlyHttpError.ts b/src/util/errors/MovedPermanentlyHttpError.ts index 70f88f243..32e438b56 100644 --- a/src/util/errors/MovedPermanentlyHttpError.ts +++ b/src/util/errors/MovedPermanentlyHttpError.ts @@ -3,6 +3,7 @@ import { RedirectHttpError } from './RedirectHttpError'; /** * Error used for resources that have been moved permanently. + * Methods other than GET may or may not be changed to GET in subsequent requests. */ export class MovedPermanentlyHttpError extends RedirectHttpError { public constructor(location: string, message?: string, options?: HttpErrorOptions) { diff --git a/src/util/errors/PermanentRedirectHttpError.ts b/src/util/errors/PermanentRedirectHttpError.ts new file mode 100644 index 000000000..f9e809c65 --- /dev/null +++ b/src/util/errors/PermanentRedirectHttpError.ts @@ -0,0 +1,16 @@ +import type { HttpErrorOptions } from './HttpError'; +import { RedirectHttpError } from './RedirectHttpError'; + +/** + * Error used for resources that have been moved permanently. + * Method and body should not be changed in subsequent requests. + */ +export class PermanentRedirectHttpError extends RedirectHttpError { + public constructor(location: string, message?: string, options?: HttpErrorOptions) { + super(308, location, 'PermanentRedirectHttpError', message, options); + } + + public static isInstance(error: any): error is PermanentRedirectHttpError { + return RedirectHttpError.isInstance(error) && error.statusCode === 308; + } +} diff --git a/src/util/errors/SeeOtherHttpError.ts b/src/util/errors/SeeOtherHttpError.ts new file mode 100644 index 000000000..0fb6b8b88 --- /dev/null +++ b/src/util/errors/SeeOtherHttpError.ts @@ -0,0 +1,17 @@ +import type { HttpErrorOptions } from './HttpError'; +import { RedirectHttpError } from './RedirectHttpError'; + +/** + * Error used to redirect not to the requested resource itself, but to another page, + * for example a representation of a real-world object. + * The method used to display this redirected page is always GET. + */ +export class SeeOtherHttpError extends RedirectHttpError { + public constructor(location: string, message?: string, options?: HttpErrorOptions) { + super(303, location, 'SeeOtherHttpError', message, options); + } + + public static isInstance(error: any): error is SeeOtherHttpError { + return RedirectHttpError.isInstance(error) && error.statusCode === 303; + } +} diff --git a/src/util/errors/TemporaryRedirectHttpError.ts b/src/util/errors/TemporaryRedirectHttpError.ts new file mode 100644 index 000000000..559f60fe4 --- /dev/null +++ b/src/util/errors/TemporaryRedirectHttpError.ts @@ -0,0 +1,16 @@ +import type { HttpErrorOptions } from './HttpError'; +import { RedirectHttpError } from './RedirectHttpError'; + +/** + * Error used for resources that have been moved temporarily. + * Method and body should not be changed in subsequent requests. + */ +export class TemporaryRedirectHttpError extends RedirectHttpError { + public constructor(location: string, message?: string, options?: HttpErrorOptions) { + super(307, location, 'TemporaryRedirectHttpError', message, options); + } + + public static isInstance(error: any): error is TemporaryRedirectHttpError { + return RedirectHttpError.isInstance(error) && error.statusCode === 307; + } +} diff --git a/test/unit/util/errors/RedirectHttpError.test.ts b/test/unit/util/errors/RedirectHttpError.test.ts index 5536c86fc..5011e7bff 100644 --- a/test/unit/util/errors/RedirectHttpError.test.ts +++ b/test/unit/util/errors/RedirectHttpError.test.ts @@ -1,7 +1,10 @@ import { FoundHttpError } from '../../../../src/util/errors/FoundHttpError'; import type { HttpErrorOptions } from '../../../../src/util/errors/HttpError'; import { MovedPermanentlyHttpError } from '../../../../src/util/errors/MovedPermanentlyHttpError'; +import { PermanentRedirectHttpError } from '../../../../src/util/errors/PermanentRedirectHttpError'; import { RedirectHttpError } from '../../../../src/util/errors/RedirectHttpError'; +import { SeeOtherHttpError } from '../../../../src/util/errors/SeeOtherHttpError'; +import { TemporaryRedirectHttpError } from '../../../../src/util/errors/TemporaryRedirectHttpError'; class FixedRedirectHttpError extends RedirectHttpError { public constructor(location: string, message?: string, options?: HttpErrorOptions) { @@ -13,6 +16,9 @@ describe('RedirectHttpError', (): void => { const errors: [string, number, typeof FixedRedirectHttpError][] = [ [ 'MovedPermanentlyHttpError', 301, MovedPermanentlyHttpError ], [ 'FoundHttpError', 302, FoundHttpError ], + [ 'SeeOtherHttpError', 303, SeeOtherHttpError ], + [ 'TemporaryRedirectHttpError', 307, TemporaryRedirectHttpError ], + [ 'PermanentRedirectHttpError', 308, PermanentRedirectHttpError ], ]; describe.each(errors)('%s', (name, statusCode, constructor): void => {