Errors

In this guide, we'll explore how errors are handled in the Dacosoft API and what different error responses mean. Understanding these errors will help you debug issues and implement proper error handling in your applications.

You can determine if your request was successful by checking the status code and message in the API response. Every response includes a status code and a message that helps identify what went wrong when an error occurs.


Status codes

Here are the status codes you might encounter when using the Dacosoft API:

  • Name
    200
    Description

    Successful request. The API processed your request as expected.

  • Name
    401
    Description

    Unauthorized. Your API key is missing or invalid.

  • Name
    403
    Description

    Forbidden. Your account doesn't have the required plan or API access.

  • Name
    500
    Description

    Internal Server Error. Something went wrong on our end.


Error Responses

When a request fails, the Dacosoft API returns a consistent error response format. The response includes:

  • A status code indicating the type of error
  • A message describing what went wrong

Common error scenarios include:

  • Name
    Invalid API Key
    Description

    The provided x-api-key header is missing or invalid.

  • Name
    Plan Restrictions
    Description

    Your account doesn't have an enterprise plan with API integration or always_free status.

  • Name
    Missing Parameters
    Description

    Required parameters (assistant_id, messages, or lang) are missing from the request.

Error response format

{
  "status": 500,
  "message": "Internal Server Error"
}

Error Handling Examples

Here are examples of proper error handling in different programming languages:

// Using async/await with fetch
async function makeApiRequest() {
  try {
    const response = await fetch('https://dacosoft.pro/api/chat-call', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': 'your-api-key'
      },
      body: JSON.stringify({
        assistant_id: 'your-assistant-id',
        messages: [],
        lang: 'en'
      })
    });

    const data = await response.json();
    
    if (data.status !== 200) {
      throw new Error(data.message || 'API error occurred');
    }

    return data;
  } catch (error) {
    if (error instanceof SyntaxError) {
      console.error('Invalid JSON response:', error);
    } else if (error instanceof TypeError) {
      console.error('Network error:', error);
    } else {
      console.error('API error:', error.message);
    }
    throw error;
  }
}

// Using axios
const axios = require('axios');

async function makeApiRequestAxios() {
  try {
    const response = await axios({
      method: 'post',
      url: 'https://dacosoft.pro/api/chat-call',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': 'your-api-key'
      },
      data: {
        assistant_id: 'your-assistant-id',
        messages: [],
        lang: 'en'
      }
    });

    if (response.data.status !== 200) {
      throw new Error(response.data.message || 'API error occurred');
    }

    return response.data;
  } catch (error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      console.error('API error:', error.response.data);
    } else if (error.request) {
      // The request was made but no response was received
      console.error('Network error:', error.message);
    } else {
      // Something happened in setting up the request
      console.error('Request setup error:', error.message);
    }
    throw error;
  }
}

// Promise-based approach with fetch
function makeApiRequestPromise() {
  return fetch('https://dacosoft.pro/api/chat-call', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-api-key': 'your-api-key'
    },
    body: JSON.stringify({
      assistant_id: 'your-assistant-id',
      messages: [],
      lang: 'en'
    })
  })
    .then(response => response.json())
    .then(data => {
      if (data.status !== 200) {
        throw new Error(data.message || 'API error occurred');
      }
      return data;
    })
    .catch(error => {
      if (error instanceof SyntaxError) {
        console.error('Invalid JSON response:', error);
      } else if (error instanceof TypeError) {
        console.error('Network error:', error);
      } else {
        console.error('API error:', error.message);
      }
      throw error;
    });
}

// Usage examples
// 1. Using async/await
async function example1() {
  try {
    const result = await makeApiRequest();
    console.log('Success:', result);
  } catch (error) {
    console.error('Failed:', error);
  }
}

// 2. Using promises
makeApiRequestPromise()
  .then(result => console.log('Success:', result))
  .catch(error => console.error('Failed:', error));

// 3. Using axios
async function example3() {
  try {
    const result = await makeApiRequestAxios();
    console.log('Success:', result);
  } catch (error) {
    console.error('Failed:', error);
  }
}

Error Handling Best Practices

When working with the API, implement these error handling practices:

  1. Always check the response status code
  2. Include the x-api-key header in every request
  3. Verify your account has the correct plan and API access
  4. Include all required parameters in the request body
  5. Implement proper error handling in your code
  6. Use try-catch blocks to handle network and parsing errors
  7. Log errors appropriately in your application

Was this page helpful?