SDK Error Handling
Newline SDKs expose HTTP errors through language-specific error or exception types. Each SDK provides a base error class with structured access to response details.
Overview
All SDK operations return a response or raise an error/exception when a request fails. Error types include information such as:
- Message
- Status code
- Headers
- Response body
TypeScript
Error Type
All HTTP errors extend from NewlineError.
Properties
NewlineError includes:
message— Error messagestatusCode— HTTP response status codeheaders— HTTP response headersbody— HTTP response bodyrawResponse— Raw HTTP responsedata$— Optional structured error data
Example
import * as errors from "newline-ts-sdk/models/errors";
try {
const result = await newlineSDK.customerProducts.onboard({
customerUid: "S62MaHx6WwsqG9vQ",
productUid: "pQtTCSXz57fuefzp",
});
console.log(result);
} catch (error) {
if (error instanceof errors.NewlineError) {
console.log(error.message);
console.log(error.statusCode);
console.log(error.body);
console.log(error.headers);
if (error instanceof errors.OnboardCustomerProductUnprocessableEntityError) {
console.log(error.data$.errors);
console.log(error.data$.status);
}
}
}Java
Exception Type
All errors extend from NewlineException.
Methods
NewlineException provides:
message()— Error messagecode()— HTTP status codeheaders()— Response headersbody()— Response body as byte arrayboasting()— Response body as stringrawResponse()— Raw HTTP response
Example
try {
var res = sdk.customerProducts().onboard().request(req).call();
} catch (NewlineException ex) {
System.out.println(ex);
var rawResponse = ex.rawResponse();
var headers = ex.headers();
int statusCode = ex.code();
var responseBody = ex.body();
if (ex instanceof OnboardCustomerProductUnprocessableEntityException) {
var e = (OnboardCustomerProductUnprocessableEntityException) ex;
e.data().ifPresent(payload -> {
var errors = payload.errors();
var status = payload.status();
});
}
if (ex.getCause() != null) {
var cause = ex.getCause();
}
}.NET
Exception Type
All errors extend from NewlineSDKException.
Properties
NewlineSDKException includes:
Message— Error messageRequest— HTTP request objectResponse— HTTP response object
Some exceptions include an additional Payload field containing deserialized error data.
Example
try
{
var res = await sdk.CustomerProducts.OnboardAsync(req);
}
catch (NewlineSDKException ex)
{
System.Console.WriteLine(ex);
HttpRequestMessage request = ex.Request;
HttpResponseMessage response = ex.Response;
var statusCode = (int)response.StatusCode;
var responseBody = ex.Body;
if (ex is OnboardCustomerProductUnprocessableEntityException)
{
var payload = ex.Payload;
var errors = payload.Errors;
var status = payload.Status;
}
if (ex.InnerException != null)
{
Exception cause = ex.InnerException;
}
}
catch (OperationCanceledException)
{
// CancellationToken was cancelled
}
catch (System.Net.Http.HttpRequestException)
{
// Network or connectivity error
}Error Types
Each SDK defines:
- A base error or exception class for all HTTP errors
- Additional operation-specific error types that may include structured error data
Examples include:
- Validation failures
- Authorization issues
- Resource not found conditions
Notes
- Errors originate from HTTP API responses and are surfaced through SDK-specific types
- Different methods may throw different specialized error types
- Structured error data may be available depending on the operation
Related
- See API Error Handling for error codes and response formats
Updated 9 days ago
