Skip to main content

Error Handling


Error handling is important to ensuring your applications inform an end user when something goes wrong and The DynamicPDF API is no exception.

DynamicPDF API provides error responses as a JSON message describing the error. All client libraries provide the following:

  • An HTTP status.
  • A JSON error message describing the error.
  • A boolean that indicates success or failure.

A value of false, the HTTP code, and a JSON error message are returned when a call to an endpoint fails.

important

All client libraries provide an "is successful" boolean indicating if the call to the endpoint was successful, an HTTP status code indicating the call's status, and a JSON error message if a call is unsuccessful.

Use the methods provided to ensure proper error handling when calling any DynamicPDF API endpoints.

tip

Use the following logic to ensure a call's success.

if 
pdf-response call is successfull
then
process response
else
handle JSON error response

dlex-layout Endpoint Example

Let's illustrate exception handling by considering an example that calls the dlex-layout endpoint. The dlex-layout endpoint combines a DLEX file and JSON layout data document that contains the data used by the DLEX to generate a PDF report.

Obtain this example using the error-handling-example sample available from your CloudAPI Cloud Storage using the File Manager.

  • SimpleReportCoverPage.dlex
  • SimpleResportWithCoverPage.json
  • NorthwindLogo.gif

Consider the following JSON layout data SimpleReportWithCoverPage.json used by a DLEX file to generate a report.

{
'ReportCreatedFor': 'Alex Smith',
'Products':
[
{
'ProductID': 17,
'ProductName': 'Alice Mutton',
'QuantityPerUnit': '20 - 1 kg tins',
'UnitPrice': 39,
'Discontinued': true
},
{
'ProductID': 3,
'ProductName': 'Aniseed Syrup',
'QuantityPerUnit': '12 - 550 ml bottles',
'UnitPrice': 10,
'Discontinued': false
}
]
}

The following example uses cURL to generate the PDF report

curl https://api.dpdf.io/v1.0/dlex-layout 
-H "Authorization:Bearer DP.xxx-api-key-xxx"
-F "DlexPath=samples/error-handling-example/SimpleReportWithCoverPage.dlex"
-F "LayoutData=@C:/temp/dynamicpdf-api-samples/error-handling-example/SimpleReportWithCoverPage.json"
-o simple-report.pdf

The report produced is a simple report and cover page.

Now let's illustrate error handling by introducing errors to the dataset. Error handling is described below when using cURL, C#, Java, NodeJS, PHP, Go, and Python.

Error Handling

In this first example, the call to the dlex-layout endpoint omits the Authorization key.

curl https://api.dpdf.io/v1.0/dlex-layout 
-H "Authorization:Bearer DP.xxx-api-key-xxx"
-F "DlexPath=samples/error-handling-example/SimpleReportWithCoverPage.dlex"
-F "LayoutData=@C:/temp/dynamicpdf-api-samples/error-handling-example/SimpleReportWithCoverPage.json"
-o simple-report.pdf

After running the command, API returns an error JSON response. But cURL writes the error message to the file specified (simple-report.pdf). An error occurs if you attempt to open the PDF in Acrobat.

Opening the file in a text editor reveals the JSON error as text. The omitted Authorization key results in the following error message.

{
"type":"https://tools.ietf.org/html/rfc7235#section-3.1",
"title":"Unauthorized",
"status":401,
"traceId":"00-ec2b7a11f140abf9988acdcad41372d7-c8425194c618c6e9-00"
}
important

cURL writes the DynamicPDF API's JSON to the file specified by the cURL command.

This next example adds an Authorization key but has an incorrect DlexPath.

curl https://api.dpdf.io/v1.0/dlex-layout 
-H "Authorization:Bearer DP.xxx-api-key-xxx"
-F "DlexPath=samples/wrong-path/SimpleReportWithCoverPage.dlex"
-F "LayoutData=@C:/temp/dynamicpdf-api-samples/error-handling-example/SimpleReportWithCoverPage.json"
-o simple-report.pdf

Executing the cURL command results in the following error message.

{
"id":"f0b5d157-f9b0-4a89-bd20-eab54df59271",
"message":"Array cannot be null. (Parameter 'bytes')"
}

The final example, modifies the JSON layout data presented above to be invalid.

false}xx]}

Executing the cURL command results in the following error message.

{
"id":"2f7e6e47-a60a-4e84-937f-268ca90ce6b0",
"message":"After parsing a value an unexpected character was encountered: x. Path 'Products[1]', line 1, position 296."
}
   Follow us on social media for latest news!