Error handling

HTTP-error codes

In this part, we will briefly go through the possible HTTP-error codes that can be returned by Network as Code API.

Some of these errors can be general, like trying to test an SDK that calls an API you haven't subscribed to yet, using invalid parameters in a request, connection errors, etc. While other errors might be more specific, for example, using a personal API key instead of the authorized organization's one.

Having said that, we hope our error descriptions will help you figure out the cause of the error you are getting and solve it fast and easily!

Importing exception types

Network as Code SDK has a sub-module for exception types (network_as_code.errors). Here's how you can import an exception from it:

from network_as_code.errors import AuthenticationException, APIError, NotFound, ServiceError
 
try:
    # Let's try to retrieve a location with the maximum age parameter set to 1 hour:
    location = device.Location(max_age=3600)
except AuthenticationException as error:
    print("Failed to authenticate.")
except APIError as error:
    print("An invalid input was passed or there was an error in the Network as Code API.")
except NotFound as error:
    print("Resource can't be found from the Network as Code API.")
except ServiceError as error:
    print("There was an internal server error.")

4xx client errors

400 and 422 API errors (invalid input)

These statuses are more general and can indicate an invalid parameter was passed, some value is out of range or not compliant with the API schema, for example. Then, our SDK defines exception classes for these scenarios that look like this:

class APIError(NaCError):
    """Error for when the Network as Code API returns an error message."""
 
class InvalidParameter(NaCError):
    """Error for when the user input parameters are invalid"""

Troubleshooting

401 and 403 authentication exceptions (forbidden)

These errors are related to authentication exceptions, which means you need to authorize yourself or get authorization from an organization and use the right API keys.

TIP: Refer to the Consent and identity management document to make sure you or the enterprise you are working with has the proper authorization to use Network as Code features on authorized devices.

Additionally, always subscribe to the APIs you need to use. Then, our SDK defines an exception class for this scenario that looks like this:

class AuthenticationException(NaCError):
    """Error for when the API key is invalid, the user of the key is not subscribed to the API, or the API key was not supplied. (403)"""

Troubleshooting

  • First, make sure you have completed the steps for initial setup and getting your app keys.

  • You must subscribe to the necessary APIs, before you test them. Otherwise, you might get a "forbidden" error message.

  • If you are working under an organization, make sure you are authorized to use their default application key or that your personal key has the necessary authorization to use resources. This is due to consent and identity security reasons.

404 not-found errors

Let's suppose you are trying to get the connectivity status for a device using an ID that doesn't exist anymore, because it was deleted. In this case, our SDK defines an exception class for this scenario that looks like this:

class NotFound(NaCError):
    """Error for when a resource can't be found from the Network as Code API."""

Troubleshooting

  • Make sure you have created a session or that it (still) exists.

  • Similarly, have you checked the device ID you are using is a valid or existing one? Include at least a valid one.

  • How about checking the QoS profiles you are using? We have listed all the available ones here.

406 bad-acceptable errors

This error usually occurs when a client tries to send a request with an Accept header specifying a content type the server cannot process. For example, if the client requests a response other than JSON. Then, the server might not be able to produce an appropriate one through the Accept headers.

  • Check that you have the correct Accept header in your request
  • Review the SDK and API documentation and make sure to use the correct content type
  • Try contacting our Network as Code support e-mail address for further assistance.

5xx server errors

500 internal errors

These are internal server errors and they can be triggered by incorrect parameters, but it is always an error on the API side. Our SDK defines exception classes for these scenarios that look like this:

class ServiceError(NaCError):
    """Error for when the server returns an error when responding to the request. (5XX)"""
 
class APIConnectionError(NaCError):
    """Error for when the Network as Code API cannot be reached."""

Troubleshooting

502 bad-gateway errors

These errors can be caused by gateway, API or network connectivity issues, server overload, or misconfigured firewalls. In the context Network as Code APIs, it typically occurs when a server that is acting as a gateway or proxy cannot connect to an upstream server and fulfill a request.

In this case, our SDK defines an exception class for this scenario that looks like this:

class GatewayConnectionError(NaCError):
    """Error for when a connection to the SDK gateway can't be established."""

Troubleshooting

  • You can try reloading the page or retry the request and contact our support in case it still does not work.

503 service unavailable errors

These refer to more general errors. Typically, the server might just be down, and the service might be unavailable temporarily.

Troubleshooting

  • You can try reloading the page or retry the request and contact our support in case it still does not work.

Last updated on July 09, 2024