Every Camera operation in Darkgrade Slate fails in one of three ways. The class tells you whether anything reached the camera, and so whether a retry is safe.
import {
Camera,
CameraOutcomeUnknownError,
CameraRefusedError,
CameraUnsupportedError,
} from '@darkgrade/slate'
const camera = new Camera()
await camera.connect()
try {
await camera.setIso('ISO 800')
} catch (error) {
if (error instanceof CameraUnsupportedError) {
// Nothing was sent. getCapabilities() says why.
} else if (error instanceof CameraRefusedError) {
// The camera said no, or doesn't offer that value now.
// Nothing changed.
} else if (error instanceof CameraOutcomeUnknownError) {
// Sent, not confirmed. Read the camera before you send it again.
console.log(await camera.getIso())
} else {
throw error
}
} finally {
await camera.disconnect()
}The three errors
CameraUnsupportedError: this camera can't do it right now. Nothing was sent.
Retry? Not as it stands. ReadgetCapabilities():operationsgives the reason for a method,controlsshows whether a setting is there and writable. Change the mode or lens, then call again.CameraRefusedError: the camera said no, or the value isn't one it offers now. Nothing changed.
Retry? Yes, with a value from the setting'sallowedValues, or once the camera is no longer busy.CameraOutcomeUnknownError: the command was sent but its result could not be confirmed. Slate never resends it.
Retry? Only after you read the camera's state. A second shutter or a second Stop is yours to decide.
Each error has operation: the method name ('takePhoto') or the setting id ('iso') that failed, so you can report it without parsing message. When a transport or protocol failure is behind it, that error is cause.
Two errors are kinds of CameraOutcomeUnknownError, so one instanceof check catches them:
CameraConfirmationError: the camera acknowledged a write, but a fresh read did not show the requested state. It hasexpectedandobserved.CameraClipNotFoundError: the camera stopped recording, but no new clip appeared on the card in time. The footage stays on the card:listMedia()finds it later.
Other errors
A few failures keep their own class, which is why the example above ends with throw error:
- An argument of the wrong shape.
connect()andon()throwTypeError, for example forconnect({ usb: … })in place ofconnect({ device }). Nothing is sent. - No camera to open.
connect()throws a plainErrorwhen no USB device matches, or when several do:Multiple USB cameras match; select a unique serialNumber before connecting. See Choose a camera. - A protocol error that was not wrapped. Some reads rethrow a
PTPErroras it is, such as a timeout duringgetBattery(). Handle it like the same class as acause.
A camera that stopped answering
A timeout or a broken reply leaves a half-finished exchange on the cable. On USB, Slate resets the connection by itself before the next command: see After a timeout. When that isn't enough, call recover():
const report = await camera.recover()
if (report.recovered) {
await camera.connect() // recover() leaves the camera closed
} else if (report.needsPhysicalAction) {
console.error('Power-cycle the camera or replug the cable')
}
console.log(report.steps)
// [{ step: 'session', ok: true, durationMilliseconds: 412 }]recover() tries a fresh session, then a PTP Device Reset, then a USB port reset, and stops at the first step after which the camera answers. The options and the full report are in Recover a stuck camera.
needsPhysicalAction: true is the only signal that power or hub cycling is warranted. A busy camera, a refusal or a single timeout is not.
Read the cause
cause holds the protocol error behind a public error. All of them extend PTPError; the Errors reference lists their fields.
PTPTimeoutError: the camera did not answer in time and may still be working. Read its state before you repeat anything.PTPResponseError: the camera answered with a response code, incode. Nothing is wrong with the connection.PTPProtocolErrorandPTPDecodeError: the reply broke PTP's framing or could not be decoded. Read the camera's state before you repeat anything.PTPDisconnectedError: the cable was pulled, the camera turned off or the socket closed. Callconnect()again.PTPRecoveryRequiredError: an earlier failure left the connection unusable. Calldisconnect()andconnect(), orrecover().
import {
CameraOutcomeUnknownError,
PTPDisconnectedError,
PTPTimeoutError,
} from '@darkgrade/slate'
try {
await camera.getInfo()
} catch (error) {
if (error instanceof CameraOutcomeUnknownError) {
const { cause } = error
if (cause instanceof PTPDisconnectedError) {
console.error('The camera went away')
}
if (cause instanceof PTPTimeoutError) {
console.error(`No ${cause.phase} in time`)
}
}
}DeviceBusy (0x2019)
- Cause. The camera can't start this operation right now, usually because it is still finishing something else, such as writing a photo to the card or changing mode. PTP defines DeviceBusy as "not performed", so nothing changed.
- What Slate does. It sends a standard PTP operation again by itself: up to 6 retries, waiting 50 ms and doubling each time, for at most 2 seconds. You see the code only when the camera is still busy after that.
transactionPolicychanges those limits. - Fix. Wait and call again: a retry is safe. If it keeps coming back, let the camera finish what it is doing first.
SessionAlreadyOpen (0x201E)
- Cause. The camera still holds a session from an earlier connection, usually a program that exited without closing it. A camera serves one session at a time and answers OpenSession with SessionAlreadyOpen.
- What Slate does.
connect()handles it: it closes the stale session and opens a new one, so you don't normally see this code fromCamera. - Fix. With raw protocol access or another PTP library, send CloseSession and open the session again. If another program is really using the camera, close that program first: only one can hold the connection.
Sony AuthenticationFailed (0xA101)
- Cause. A Sony camera refused the handshake that starts remote control. Sony defines AuthenticationFailed as: the computer announced an older protocol version than the camera's, or authentication failed for another reason. The code is in Sony's vendor range, so it has this meaning only on a Sony.
- What Slate does. During
connect()it announces protocol version 3.00, then 2.00 for older bodies. If the camera refuses both,connect()throwsCameraRefusedErrorwhosecauseis aPTPResponseErrorwithcode0xA101. - Fix. Check the camera's USB connection mode is PC Remote (or Remote Shooting), not Mass Storage, close other apps that use the camera, replug it and connect again. If it still refuses, tell us the model and firmware version.
The messages these and other failures print in Studio, MCP, the CLI and Slate are listed in Error messages, and every standard code is in Responses.
connect, getInfo, recover and disconnect always wrap: a PTPResponseError becomes CameraRefusedError, and any other PTPError becomes CameraOutcomeUnknownError. With raw protocol access you catch these classes directly.
A camera another process is using
HardwareBusyError, from @darkgrade/slate/runtime, means a host that shares cameras between processes would not let the call run now. Wait retryAfterMilliseconds and try again: see A busy camera.
Still stuck?
Get help says where to ask and what to send.