Skip to content

Commit c0eb077

Browse files
committed
Consistent error messages
1 parent 171f7f8 commit c0eb077

23 files changed

Lines changed: 136 additions & 121 deletions

java-does-usb/src/main/java/net/codecrete/usb/USB.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ private static USBDeviceRegistry createInstance() {
3333
} else if (osName.equals("Linux") && (osArch.equals("amd64") || osArch.equals("aarch64"))) {
3434
impl = new LinuxUSBDeviceRegistry();
3535
} else {
36-
throw new UnsupportedOperationException(String.format("Java Does USB has no implementation for architecture " + "%s/%s", osName, osArch));
36+
throw new UnsupportedOperationException(String.format(
37+
"Java Does USB has no implementation for architecture %s/%s",
38+
osName, osArch));
3739
}
3840
return impl;
3941
}

java-does-usb/src/main/java/net/codecrete/usb/USBControlTransfer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
* @param value value (value between 0 and 65535, called {@code wValue} in USB specification)
2525
* @param index index (value between 0 and 65535, called {@code wIndex} in USB specification).
2626
*/
27-
public record USBControlTransfer(USBRequestType requestType, USBRecipient recipient, int request, int value,
28-
int index) {
27+
public record USBControlTransfer(
28+
USBRequestType requestType,
29+
USBRecipient recipient,
30+
int request,
31+
int value,
32+
int index
33+
) {
2934
}

java-does-usb/src/main/java/net/codecrete/usb/common/CompositeFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
package net.codecrete.usb.common;
99

1010
/**
11-
* Describes the function of an interface of a composite USB device.
11+
* Describes a function of a composite USB device.
1212
* <p>
1313
* A composite USB device can have multiple functions, e.g. a mass
1414
* storage function and a virtual serial port function. Each function

java-does-usb/src/main/java/net/codecrete/usb/common/ConfigurationParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public class ConfigurationParser {
2929

3030
/**
31-
* Parse a USB configuration descriptor (incl. interface and endpoint descriptors)
31+
* Parses a USB configuration descriptor (incl. interface and endpoint descriptors)
3232
*
3333
* @param desc configuration descriptor
3434
* @return parsed configuration data
@@ -97,11 +97,11 @@ public Configuration parse() {
9797
private void parseHeader() {
9898
var desc = new ConfigurationDescriptor(descriptor);
9999
if (CONFIGURATION_DESCRIPTOR_TYPE != desc.descriptorType())
100-
throw new USBException("Invalid USB configuration descriptor");
100+
throw new USBException("invalid USB configuration descriptor");
101101

102102
var totalLength = desc.totalLength();
103103
if (descriptor.byteSize() != totalLength)
104-
throw new USBException("Invalid USB configuration descriptor length");
104+
throw new USBException("invalid USB configuration descriptor (invalid length)");
105105

106106
configuration = new Configuration(desc.configurationValue(), desc.attributes(), desc.maxPower());
107107
}

java-does-usb/src/main/java/net/codecrete/usb/common/EndpointInputStream.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
* Input stream for bulk endpoints – optimized for high throughput.
2323
*
2424
* <p>
25-
* Multiple asynchronous transfer are submitted to achieve a good
25+
* Multiple asynchronous transfers are submitted to achieve a good
2626
* degree of concurrency between the USB communication handled by the operating
2727
* system and the consuming application code.
2828
* </p>
2929
* <p>
30-
* For thread synchronization (between the background thread handling IO completion
30+
* For thread synchronization (between the background thread handling IO completions
3131
* and the consuming application thread) a blocking queue is used. When an transfer
3232
* completes, the background thread adds it to the queue. The consuming code
3333
* waits for the next item in the queue.
@@ -178,7 +178,7 @@ private void receiveMoreData() throws IOException {
178178

179179
// check for error
180180
if (currentTransfer.resultCode() != 0)
181-
device.throwOSException(currentTransfer.resultCode(), "error reading from endpoint %d",
181+
device.throwOSException(currentTransfer.resultCode(), "error occurred while reading from endpoint %d",
182182
endpointNumber);
183183

184184
} while (currentTransfer.resultSize() <= 0);

java-does-usb/src/main/java/net/codecrete/usb/common/EndpointOutputStream.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class EndpointOutputStream extends OutputStream {
4040
protected USBDeviceImpl device;
4141
protected final int endpointNumber;
4242
protected final Arena arena;
43-
// Endpoint's packet size
43+
// Endpoint packet size
4444
private final int packetSize;
4545
// Transfer size (multiple of packet size)
4646
private final int transferSize;
@@ -228,7 +228,7 @@ private Transfer waitForAvailableTransfer() {
228228
var result = transfer.resultCode();
229229
if (result != 0 && !hasError) {
230230
transfer.setResultCode(0);
231-
device.throwOSException(result, "error writing to endpoint %d", endpointNumber);
231+
device.throwOSException(result, "error occurred while transmitting to endpoint %d", endpointNumber);
232232
}
233233

234234
return transfer;
@@ -256,6 +256,6 @@ protected void configureEndpoint() {
256256

257257
private void checkIsOpen() throws IOException {
258258
if (isClosed())
259-
throw new IOException("Bulk endpoint output stream has been closed");
259+
throw new IOException("endpoint output stream has been closed");
260260
}
261261
}

java-does-usb/src/main/java/net/codecrete/usb/common/TransferCompletion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public interface TransferCompletion {
1616
/**
1717
* Called when the asynchronous transfer has completed.
1818
* <p>
19-
* When this function has been called, {@link Transfer#resultCode} and
20-
* {@link Transfer#resultSize} have been filled in.
19+
* When this function has been called, {@link Transfer#resultSize()} and
20+
* {@link Transfer#resultSize()} have been filled in.
2121
* </p>
2222
* <p>
2323
* Since there is a single background task calling all completion functions,

java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceImpl.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public abstract class USBDeviceImpl implements USBDevice {
2222

2323
/**
24-
* Operation-specific device ID used for {@link #equals(Object)} and {@link #hashCode()}.
24+
* Operating system-specific device ID used for {@link #equals(Object)} and {@link #hashCode()}.
2525
* <p>
2626
* Can be a String instance (such as a file path) or a Long instance (such as an internal ID).
2727
* It only needs to be valid for the duration the device is connected.
@@ -79,7 +79,7 @@ public void attachStandardDrivers() {
7979

8080
protected void checkIsOpen() {
8181
if (!isOpen())
82-
throw new USBException("The device needs to be open to call this method");
82+
throw new USBException("device needs to be opened first for this operation");
8383
}
8484

8585
@Override
@@ -221,7 +221,7 @@ public void setClaimed(int interfaceNumber, boolean claimed) {
221221
return;
222222
}
223223
}
224-
throw new USBException("Internal error (interface not found)");
224+
throw new USBException("internal error (interface not found)");
225225
}
226226

227227
@Override
@@ -232,11 +232,11 @@ public USBInterfaceImpl getInterface(int interfaceNumber) {
232232
public USBInterfaceImpl getInterfaceWithCheck(int interfaceNumber, boolean isClaimed) {
233233
var intf = getInterface(interfaceNumber);
234234
if (intf == null)
235-
throw new USBException(String.format("Invalid interface number: %d", interfaceNumber));
235+
throw new USBException(String.format("invalid interface number: %d", interfaceNumber));
236236
if (isClaimed && !intf.isClaimed()) {
237-
throw new USBException(String.format("Interface %d has not been claimed", interfaceNumber));
237+
throw new USBException(String.format("interface %d must be claimed first", interfaceNumber));
238238
} else if (!isClaimed && intf.isClaimed()) {
239-
throw new USBException(String.format("Interface %d has already been claimed", interfaceNumber));
239+
throw new USBException(String.format("interface %d has already been claimed", interfaceNumber));
240240
}
241241
return intf;
242242
}
@@ -292,7 +292,9 @@ protected void throwInvalidEndpointException(USBDirection direction, int endpoin
292292
transferTypeDesc = transferType1.name();
293293
else
294294
transferTypeDesc = String.format("%s or %s", transferType1.name(), transferType2.name());
295-
throw new USBException(String.format("Endpoint number %d does not exist, is not part of a claimed interface " + "or is not valid for %s transfer in %s direction", endpointNumber, transferTypeDesc, direction.name()));
295+
throw new USBException(String.format(
296+
"endpoint number %d does not exist, is not part of a claimed interface or is not valid for %s transfer in %s direction",
297+
endpointNumber, transferTypeDesc, direction.name()));
296298
}
297299

298300
protected int getInterfaceNumber(USBDirection direction, int endpointNumber) {
@@ -337,8 +339,8 @@ protected void waitForTransfer(Transfer transfer, int timeout, USBDirection dire
337339
if (hasTimedOut && transfer.resultCode() == 0) {
338340
abortTransfers(direction, endpointNumber);
339341
waitNoTimeout(transfer);
340-
throw new USBTimeoutException(getOperationDescription(direction, endpointNumber) + "aborted due to " +
341-
"timeout");
342+
throw new USBTimeoutException(getOperationDescription(direction, endpointNumber)
343+
+ "aborted due to timeout");
342344
}
343345
}
344346

@@ -381,9 +383,9 @@ private static boolean waitWithTimeout(Transfer transfer, int timeout) {
381383

382384
protected static String getOperationDescription(USBDirection direction, int endpointNumber) {
383385
if (endpointNumber == 0) {
384-
return "Control transfer";
386+
return "control transfer";
385387
} else {
386-
return String.format("Transfer %s on endpoint %d", direction.name(), endpointNumber);
388+
return String.format("transfer %s on endpoint %d", direction.name(), endpointNumber);
387389
}
388390

389391
}

java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ protected void emitOnDeviceDisconnected(USBDevice device) {
104104
onDeviceDisconnectedHandler.accept(device);
105105

106106
} catch (Exception e) {
107-
System.err.println("Warning: [JavaDoesUSB] unhandled exception in 'onDeviceDisconnected' handler - " +
108-
"ignoring");
107+
System.err.println(
108+
"Warning: [JavaDoesUSB] unhandled exception in 'onDeviceDisconnected' handler - ignoring");
109109
e.printStackTrace(System.err);
110110
}
111111
}
@@ -136,7 +136,7 @@ protected void startDeviceMonitor(Runnable monitorTask) {
136136
}
137137

138138
if (failureCause != null)
139-
throw new USBException("Initial device enumeration has failed", failureCause);
139+
throw new USBException("initial device enumeration has failed", failureCause);
140140
}
141141

142142
/**

java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxAsyncTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ synchronized void submitTransfer(LinuxUSBDevice device, int endpointAddress, USB
253253
if (IO.ioctl(device.fileDescriptor(), SUBMITURB, urb, errorState) < 0) {
254254
var action = endpointAddress >= 128 ? "reading from" : "writing to";
255255
var endpoint = endpointAddress == 0 ? "control endpoint" : String.format("endpoint %d", endpointAddress);
256-
throwLastError(errorState, "failed %s %s", action, endpoint);
256+
throwLastError(errorState, "error occurred while %s %s", action, endpoint);
257257
}
258258
}
259259
}
@@ -310,7 +310,7 @@ synchronized void abortTransfers(LinuxUSBDevice device, byte endpointAddress) {
310310
if (IO.ioctl(fd, DISCARDURB, urb, errorState) < 0) {
311311
// ignore EINVAL; it occurs if the URB has completed at the same time
312312
if (Linux.getErrno(errorState) != errno.EINVAL())
313-
throwLastError(errorState, "failed to abort transfer");
313+
throwLastError(errorState, "error occurred while aborting transfer");
314314
}
315315
}
316316
}

0 commit comments

Comments
 (0)