Function parseError

  • Parses and standardizes error objects

    This function takes any error object and converts it to a standardized format with consistent properties for error handling throughout the SDK. It ensures that all errors have the same structure regardless of their source.

    Parameters

    • error: any

      Any error object to parse and standardize

    Returns Promise<any>

    Promise that rejects with a standardized error object

    Basic error parsing:

    try {
    // Some operation that might fail
    await someApiCall();
    } catch (error) {
    const standardizedError = await parseError(error);
    console.log('Error message:', standardizedError.message);
    console.log('Status code:', standardizedError.status_code);
    console.log('Status:', standardizedError.status);
    }

    Error handling in API calls:

    async function safeApiCall() {
    try {
    const response = await fetch('/api/data');
    if (!response.ok) {
    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
    }
    return await response.json();
    } catch (error) {
    const standardizedError = await parseError(error);
    console.error('API Error:', {
    message: standardizedError.message,
    status: standardizedError.status,
    code: standardizedError.status_code,
    details: standardizedError.more_info
    });
    throw standardizedError;
    }
    }

    Custom error handling:

    async function handleDatabaseError(error: any) {
    const standardizedError = await parseError(error);

    if (standardizedError.status_code === 404) {
    console.log('Resource not found');
    } else if (standardizedError.status_code >= 500) {
    console.log('Server error occurred');
    } else {
    console.log('Client error:', standardizedError.message);
    }

    return standardizedError;
    }