mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add more redirect flavours
Signed-off-by: Wouter Termont <woutermont@gmail.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
16
src/util/errors/PermanentRedirectHttpError.ts
Normal file
16
src/util/errors/PermanentRedirectHttpError.ts
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
17
src/util/errors/SeeOtherHttpError.ts
Normal file
17
src/util/errors/SeeOtherHttpError.ts
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
16
src/util/errors/TemporaryRedirectHttpError.ts
Normal file
16
src/util/errors/TemporaryRedirectHttpError.ts
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user