Skip to main content
The SDK provides detailed error handling through NoCloudAPIError and the NoCloudError enum.

Basic Error Handling

import { NoCloud, NoCloudAPIError } from "@nocloud/sdk";

const cloud = new NoCloud("your-api-key");

try {
  await cloud.storage.upload(file);
} catch (error) {
  if (error instanceof NoCloudAPIError) {
    console.error(`API Error: ${error.message}`);
    console.error(`Status: ${error.status}`);
    console.error(`Code: ${error.code}`);
  }
}

Check for Specific Errors

Use the static isError method to check for specific error types:
import { NoCloudAPIError, NoCloudError } from "@nocloud/sdk";

try {
  await cloud.storage.upload(file);
} catch (error) {
  if (NoCloudAPIError.isError(error, NoCloudError.RATE_LIMIT_EXCEEDED)) {
    console.log("Rate limited, retry later");
  } else if (NoCloudAPIError.isError(error, NoCloudError.INVALID_API_KEY)) {
    console.log("Check your API key");
  } else if (NoCloudAPIError.isError(error)) {
    console.log(`Other API error: ${error.code}`);
  }
}

Error Codes

CodeHTTP StatusDescription
INVALID_API_KEY401Invalid or missing API key
BAD_REQUEST400Invalid request parameters
RATE_LIMIT_EXCEEDED429Too many requests
RESOURCE_NOT_FOUND404File or resource not found
INTERNAL_SERVER_ERROR500Server error
UNKNOWN_ERRORUnexpected error

NoCloudAPIError Properties

PropertyTypeDescription
messagestringHuman-readable error message
statusnumberHTTP status code
codeNoCloudErrorError code enum value

Retry Logic

The SDK includes built-in retry logic for transient failures. You can configure the retry behavior when initializing:
const cloud = new NoCloud({
  apiKey: "your-api-key",
  retries: 5, // Retry up to 5 times
  retryDelayMs: 2000 // Wait 2 seconds between retries
});
The SDK will automatically retry failed requests for transient errors (like network issues). It will not retry for client errors like invalid API keys or bad requests.

Handling Rate Limits

When you exceed the rate limit, the SDK throws a RATE_LIMIT_EXCEEDED error:
import { NoCloudAPIError, NoCloudError } from "@nocloud/sdk";

async function uploadWithRetry(file: File, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await cloud.storage.upload(file);
    } catch (error) {
      if (NoCloudAPIError.isError(error, NoCloudError.RATE_LIMIT_EXCEEDED)) {
        // Wait before retrying
        await new Promise((resolve) => setTimeout(resolve, 5000));
        continue;
      }
      throw error;
    }
  }
  throw new Error("Max retries exceeded");
}