Error Handling
6 min
handle configuration, http responses, transport failures and cleanup failures separately sdk version 1 0 0 does not define an allocation wait timeout or a fixed catalog of backend error codes failure types node js the package exports these sdk errors error when it is used useful fields deviceparkconfigerror missing client configuration or invalid sdk method argument message deviceparkhttperror device park returned a non 2xx response status , body deviceparkserializationerror json serialization or parsing failed message , cause deviceparkerror base class for sdk defined errors message , cause java failure exception invalid builder or method argument illegalargumentexception http, authentication, network or request timeout failure runtimeexception with the original ioexception as its cause client close failure ioexception handle errors import { deviceparkconfigerror, deviceparkhttperror, deviceparkserializationerror } from "@devicepark/public sdk"; try { const devices = await client devices() list(); console log(devices data); } catch (error) { if (error instanceof deviceparkconfigerror) { console error("invalid sdk configuration ", error message); } else if (error instanceof deviceparkhttperror) { console error("device park request failed ", error status, error body); } else if (error instanceof deviceparkserializationerror) { console error("unexpected response format ", error message); } else if (error instanceof error && error name === "aborterror") { console error("device park request timed out"); } else { throw error; } }try { pagedto\<device> devices = client devices() list( listdevicesrequest builder() build()); devices data() foreach(system out println); } catch (illegalargumentexception configurationerror) { system err println("invalid sdk configuration " + configurationerror getmessage()); } catch (runtimeexception requesterror) { system err println("device park request failed " + requesterror getmessage()); } node js request timeout uses abortcontroller a timeout is therefore a transport level aborterror , not a deviceparkhttperror and not an allocation specific error code java sdk version 1 0 0 does not expose a structured http status field if machine readable status handling is required, agree on that behavior before coupling automation logic to exception message text allocation waiting creating an allocation and waiting for a device are separate concerns the sdk returns the allocation response; it does not keep polling until a device is assigned your runner should store allocationid immediately inspect the same allocation until deviceserial is assigned apply an application level deadline suitable for the ci job release the allocation if the deadline is exceeded do not assume a special queue timeout exception or status code none is defined by the public sdk contract set the queue deadline in your test runner the sdk does not poll automatically and does not throw a dedicated allocation timeout error cleanup cleanup order is session, allocation, client stop every active sessionid release the allocationid after the final session close the client catch cleanup failures separately so they do not replace the original test failure include resource identifiers in logs, but never include credentials, access tokens or authorization headers
