Skip to content

Commit

Permalink
feat: add Exception Class JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiofrittoli committed Jan 3, 2025
1 parent d257ba5 commit c68ab15
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import type { ErrorCode } from './code'

/**
* Interface representing the options for an Exception.
*
* @template TCode - The type of the error code.
* @extends ErrorOptions
*/
export interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions
{
code : TCode
name? : string
status? : number
/** The error code associated with the Exception. */
code: TCode
/** The name of the Exception. */
name?: string
/** The HTTP status code associated with the Exception. */
status?: number
}


/**
* Exception Class.
*
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
* @template TCode The type of the code property of the Exception. Defaults to `ErrorCode`.
*
* @extends {Error}
* @implements {ExceptionOptions<TCode>}
*/
export class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode>
{
// @ts-expect-error Type 'TMessage' is not assignable to type 'string'.ts(2416)
Expand All @@ -18,6 +36,12 @@ export class Exception<TMessage = string, TCode = ErrorCode> extends Error imple
code


/**
* Constructs a new Exception instance.
*
* @param message The message describing the exception.
* @param options Additional options for the exception.
*/
constructor( message: TMessage, options: ExceptionOptions<TCode> )
{
super( '', options )
Expand All @@ -30,6 +54,16 @@ export class Exception<TMessage = string, TCode = ErrorCode> extends Error imple
}



/**
* Determines if the provided error is an instance of the Exception class.
*
* @template TMessage The type of the message property of the Exception.
* @template TCode The type of the code property of the Exception.
*
* @param error The error to check.
* @returns `true` if the error is an instance of Exception or has a `typename` property equal to 'Exception', `false` otherwise.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static isException<TMessage = string, TCode = ErrorCode>( error: any ): error is Exception<TMessage, TCode>
{
Expand Down

0 comments on commit c68ab15

Please sign in to comment.