-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add details field for MastoError
- Loading branch information
Showing
10 changed files
with
98 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { MastoError } from './masto-error'; | ||
import { MastoError, MastoErrorDetails } from './masto-error'; | ||
|
||
/** | ||
* Mastodon forbidden error | ||
*/ | ||
export class MastoConflictError extends MastoError { | ||
readonly name = 'MastoConflictError'; | ||
readonly statusCode = 409; | ||
|
||
constructor(readonly message: string, readonly description?: string) { | ||
super(); | ||
constructor( | ||
readonly message: string, | ||
readonly description?: string, | ||
readonly details?: MastoErrorDetails, | ||
) { | ||
super(message, 409, description, details); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,41 @@ | ||
// https://github.com/tootsuite/mastodon/pull/15803 | ||
export type MastoErrorType = | ||
| 'ERR_BLOCKED' | ||
| 'ERR_UNREACHABLE' | ||
| 'ERR_TAKEN' | ||
| 'ERR_RESERVED' | ||
| 'ERR_ACCEPTED' | ||
| 'ERR_BLANK' | ||
| 'ERR_INVALID' | ||
| 'ERR_TOO_LONG' | ||
| 'ERR_TOO_SHORT' | ||
| 'ERR_INCLUSION'; | ||
|
||
export interface MastoErrorDetail { | ||
readonly error: MastoErrorType; | ||
readonly description: string; | ||
} | ||
|
||
export type MastoErrorDetails = Record<string, readonly MastoErrorDetail[]>; | ||
|
||
/** | ||
* Error object | ||
* @see https://docs.joinmastodon.org/entities/error/ | ||
*/ | ||
export abstract class MastoError extends Error { | ||
export class MastoError extends Error { | ||
/** Helper to check if the error has been thrown from Masto */ | ||
readonly isMastoError = true; | ||
/** HTTP status code */ | ||
readonly statusCode?: number; | ||
/** The error message. Equivalent for the `error` field from the Error entity */ | ||
abstract readonly message: string; | ||
/** A longer description of the error, mainly provided with the OAuth API. */ | ||
abstract readonly description?: string | null; | ||
|
||
constructor( | ||
/** The error message. Equivalent for the `error` field from the Error entity */ | ||
readonly message: string, | ||
/** HTTP status code */ | ||
readonly statusCode?: number, | ||
/** A longer description of the error, mainly provided with the OAuth API. */ | ||
readonly description?: string | null, | ||
/** Used by /api/v1/accounts */ | ||
readonly details?: MastoErrorDetails | null, | ||
) { | ||
super(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { MastoError } from './masto-error'; | ||
import { MastoError, MastoErrorDetails } from './masto-error'; | ||
|
||
/** | ||
* Mastodon forbidden error | ||
*/ | ||
export class MastoForbiddenError extends MastoError { | ||
readonly name = 'MastoForbiddenError'; | ||
readonly statusCode = 403; | ||
|
||
constructor(readonly message: string, readonly description?: string) { | ||
super(); | ||
constructor( | ||
message: string, | ||
description?: string, | ||
details?: MastoErrorDetails, | ||
) { | ||
super(message, 403, description, details); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { MastoError } from './masto-error'; | ||
import { MastoError, MastoErrorDetails } from './masto-error'; | ||
|
||
/** | ||
* Mastodon gone error | ||
*/ | ||
export class MastoGoneError extends MastoError { | ||
readonly name = 'MastoGoneError'; | ||
readonly statusCode = 410; | ||
|
||
constructor(readonly message: string, readonly description?: string) { | ||
super(); | ||
constructor( | ||
message: string, | ||
description?: string, | ||
details?: MastoErrorDetails, | ||
) { | ||
super(message, 410, description, details); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { MastoError } from './masto-error'; | ||
import { MastoError, MastoErrorDetails } from './masto-error'; | ||
|
||
/** | ||
* Mastodon not found error class | ||
*/ | ||
export class MastoNotFoundError extends MastoError { | ||
readonly statusCode = 404; | ||
readonly name = 'MastoNotFoundError'; | ||
|
||
constructor(readonly message: string, readonly description?: string) { | ||
super(); | ||
constructor( | ||
message: string, | ||
description?: string, | ||
details?: MastoErrorDetails, | ||
) { | ||
super(message, 404, description, details); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import { MastoError } from './masto-error'; | ||
import { MastoError, MastoErrorDetails } from './masto-error'; | ||
|
||
/** | ||
* Mastodon rate limit error class | ||
* @param message Message for users | ||
*/ | ||
export class MastoRateLimitError extends MastoError { | ||
readonly name = 'MastoRateLimitError'; | ||
readonly statusCode = 429; | ||
|
||
constructor( | ||
readonly message: string, | ||
message: string, | ||
/** Number of requests permitted per time period */ | ||
readonly limit: number, | ||
/** Number of requests you can still make */ | ||
readonly remaining: number, | ||
/** Timestamp when your rate limit will reset */ | ||
readonly reset: string, | ||
readonly description?: string, | ||
description?: string, | ||
details?: MastoErrorDetails, | ||
) { | ||
super(); | ||
super(message, 429, description, details); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import { MastoError } from './masto-error'; | ||
import { MastoError, MastoErrorDetails } from './masto-error'; | ||
|
||
/** | ||
* Mastodon unauthorized error class | ||
* @param message Message for users | ||
*/ | ||
export class MastoUnauthorizedError extends MastoError { | ||
readonly statusCode = 401; | ||
readonly name = 'MastoUnauthorizedError'; | ||
|
||
constructor(readonly message: string, readonly description?: string) { | ||
super(); | ||
constructor( | ||
message: string, | ||
description?: string, | ||
details?: MastoErrorDetails, | ||
) { | ||
super(message, 401, description, details); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import { MastoError } from './masto-error'; | ||
import { MastoError, MastoErrorDetails } from './masto-error'; | ||
|
||
/** | ||
* Mastodon unprocessable entity | ||
* @param message Message for users | ||
*/ | ||
export class MastoUnprocessableEntityError extends MastoError { | ||
readonly name = 'MastoUnprocessableEntityError'; | ||
readonly statusCode = 422; | ||
|
||
constructor(readonly message: string, readonly description?: string) { | ||
super(); | ||
constructor( | ||
message: string, | ||
description?: string, | ||
details?: MastoErrorDetails, | ||
) { | ||
super(message, 422, description, details); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters