> For the complete documentation index, see [llms.txt](https://docs.yield.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yield.fi/earn-with-yieldfi/integration-sdk/error-handling/error-types.md).

# Error Types

The SDK provides specialized error classes for different error scenarios. All errors extend from `SDKError`.

### SDKError (Base Class)

Base error class that all SDK errors extend from.

**Properties:**

* `message: string` - Human-readable error message
* `code: string` - Error code for programmatic handling
* `details?: any` - Additional error context

### AuthenticationError

Thrown when authentication fails.

**Common Causes:**

* Invalid credentials
* Expired tokens
* Malformed JWT tokens
* Invalid signature

**Example:**

```typescript
import { AuthenticationError } from "yieldfi-sdk";

try {
  await sdk.auth.login(credentials);
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error("Auth failed:", error.message);
    console.error("Code:", error.code);
    console.error("Details:", error.details);
  }
}
```

### NetworkError

Thrown for network-related errors.

**Properties:**

* `statusCode?: number` - HTTP status code
* `response?: any` - Response data (if available)

**Common Causes:**

* Network connectivity issues
* Request timeouts
* HTTP errors (4xx, 5xx)
* Server unavailable

**Example:**

```typescript
import { NetworkError } from "yieldfi-sdk";

try {
  await sdk.vault.getVaultByKey("yusd", 1);
} catch (error) {
  if (error instanceof NetworkError) {
    console.error("Network error:", error.message);
    console.error("Status code:", error.statusCode);
    
    if (error.statusCode === 503) {
      console.error("Service unavailable, please try again later");
    }
  }
}
```

### ValidationError

Thrown when input validation fails.

**Common Causes:**

* Missing required parameters
* Invalid parameter types
* Out-of-range values
* Invalid format

**Example:**

```typescript
import { ValidationError } from "yieldfi-sdk";

try {
  await sdk.vault.getVaults({
    page: -1, // Invalid: page must be >= 1
  });
} catch (error) {
  if (error instanceof ValidationError) {
    console.error("Validation error:", error.message);
    console.error("Invalid field:", error.details?.field);
  }
}
```

### ConfigurationError

Thrown when SDK configuration is invalid.

**Common Causes:**

* Missing required configuration
* Invalid configuration values
* Invalid gateway URL format

**Example:**

```typescript
import { ConfigurationError } from "yieldfi-sdk";

try {
  await YieldFiSDK.create({
    gatewayUrl: "", // Invalid: empty URL
  });
} catch (error) {
  if (error instanceof ConfigurationError) {
    console.error("Configuration error:", error.message);
    console.error("Details:", error.details);
  }
}
```

### Complete Error Handling Example

```typescript
import {
  SDKError,
  AuthenticationError,
  NetworkError,
  ValidationError,
  ConfigurationError,
} from "yieldfi-sdk";

async function handleApiCall() {
  try {
    const vault = await sdk.vault.getVaultByKey("yusd", 1);
    return vault;
  } catch (error) {
    if (error instanceof AuthenticationError) {
      // Handle authentication errors
      console.error("Authentication failed:", error.message);
      // Redirect to login or refresh token
      return null;
    } else if (error instanceof NetworkError) {
      // Handle network errors
      console.error("Network error:", error.message);
      if (error.statusCode === 503) {
        // Service unavailable - show retry option
        return { retry: true };
      }
      // Show error message to user
      return null;
    } else if (error instanceof ValidationError) {
      // Handle validation errors
      console.error("Invalid input:", error.message);
      // Show validation error to user
      return null;
    } else if (error instanceof ConfigurationError) {
      // Handle configuration errors
      console.error("SDK configuration error:", error.message);
      // This should not happen in production
      return null;
    } else if (error instanceof SDKError) {
      // Handle other SDK errors
      console.error("SDK error:", error.code, error.message);
      return null;
    } else {
      // Handle unexpected errors
      console.error("Unexpected error:", error);
      return null;
    }
  }
}
```

### Error Codes

Common error codes you may encounter:

* `AUTH_FAILED` - Authentication failed
* `TOKEN_EXPIRED` - Token expired
* `INVALID_TOKEN` - Invalid token format
* `NETWORK_ERROR` - Network request failed
* `TIMEOUT` - Request timeout
* `VALIDATION_ERROR` - Input validation failed
* `NOT_FOUND` - Resource not found
* `FORBIDDEN` - Access forbidden
* `SERVER_ERROR` - Server error
