FuncType - v0.9.5
    Preparing search index...

    Variable TypedError

    TypedError: <T extends ErrorCode>(
        code: T,
        message: ErrorMessage<T>,
        context: TypedErrorContext<T>,
        options?: { cause?: unknown; traceId?: string },
    ) => TypedError<T> & {
        auth: (
            resource: string,
            requiredRole?: string,
        ) => TypedError<"AUTH_REQUIRED">;
        badRequest: (
            reason: string,
            expected?: string,
        ) => TypedError<"BAD_REQUEST">;
        conflict: (
            resource: string,
            conflictingValue: string,
        ) => TypedError<"CONFLICT">;
        hasCode: <T extends ErrorCode>(
            error: TypedError<ErrorCode>,
            code: T,
        ) => error is TypedError<T>;
        internal: (errorId: string) => TypedError<"INTERNAL_ERROR">;
        isTypedError: (value: unknown) => value is TypedError<ErrorCode>;
        network: (
            url: string,
            method: string,
            statusCode?: number,
        ) => TypedError<"NETWORK_ERROR">;
        notFound: (
            resource: string,
            id: string | number,
        ) => TypedError<"NOT_FOUND">;
        permission: (
            action: string,
            resource: string,
            userId?: string,
        ) => TypedError<"PERMISSION_DENIED">;
        rateLimit: (
            limit: number,
            window: string,
            retryAfter?: number,
        ) => TypedError<"RATE_LIMITED">;
        timeout: (duration: number, operation: string) => TypedError<"TIMEOUT">;
        validation: (
            field: string,
            value: unknown,
            rule: string,
        ) => TypedError<"VALIDATION_FAILED">;
    }

    Type declaration

    • auth: (resource: string, requiredRole?: string) => TypedError<"AUTH_REQUIRED">

      Create an authentication error

      const error = TypedError.auth("/api/admin", "admin")
      // Type: TypedError<"AUTH_REQUIRED">
    • badRequest: (reason: string, expected?: string) => TypedError<"BAD_REQUEST">

      Create a bad request error

      const error = TypedError.badRequest("Invalid JSON", "valid JSON object")
      // Type: TypedError<"BAD_REQUEST">
    • conflict: (resource: string, conflictingValue: string) => TypedError<"CONFLICT">

      Create a conflict error

      const error = TypedError.conflict("email", "user@example.com")
      // Type: TypedError<"CONFLICT">
    • hasCode: <T extends ErrorCode>(
          error: TypedError<ErrorCode>,
          code: T,
      ) => error is TypedError<T>

      Check if a TypedError has a specific code

    • internal: (errorId: string) => TypedError<"INTERNAL_ERROR">

      Create an internal error

      const error = TypedError.internal("ERR-500-ABC123")
      // Type: TypedError<"INTERNAL_ERROR">
    • isTypedError: (value: unknown) => value is TypedError<ErrorCode>

      Check if a value is a TypedError

    • network: (
          url: string,
          method: string,
          statusCode?: number,
      ) => TypedError<"NETWORK_ERROR">

      Create a network error

      const error = TypedError.network("https://api.example.com", "POST", 500)
      // Type: TypedError<"NETWORK_ERROR">
    • notFound: (resource: string, id: string | number) => TypedError<"NOT_FOUND">

      Create a not found error

      const error = TypedError.notFound("user", "123")
      // Type: TypedError<"NOT_FOUND">
    • permission: (
          action: string,
          resource: string,
          userId?: string,
      ) => TypedError<"PERMISSION_DENIED">

      Create a permission denied error

      const error = TypedError.permission("delete", "post", "user123")
      // Type: TypedError<"PERMISSION_DENIED">
    • rateLimit: (
          limit: number,
          window: string,
          retryAfter?: number,
      ) => TypedError<"RATE_LIMITED">

      Create a rate limit error

      const error = TypedError.rateLimit(100, "1h", 3600)
      // Type: TypedError<"RATE_LIMITED">
    • timeout: (duration: number, operation: string) => TypedError<"TIMEOUT">

      Create a timeout error

      const error = TypedError.timeout(30000, "database query")
      // Type: TypedError<"TIMEOUT">
    • validation: (field: string, value: unknown, rule: string) => TypedError<"VALIDATION_FAILED">

      Create a validation error

      const error = TypedError.validation("email", "test@", "must be valid email")
      // Type: TypedError<"VALIDATION_FAILED">
      // Message must match: "Validation failed: ..."