Skip to content

Report writeBytes errors via java exceptions - #146

Merged
tresf merged 2 commits into
java-native:masterfrom
hiddenalpha:report-write-bytes-errors-as-exceptions-20231108
Nov 8, 2023
Merged

tresf merged 2 commits into
java-native:masterfrom
hiddenalpha:report-write-bytes-errors-as-exceptions-20231108

Conversation

@hiddenalpha

Copy link
Copy Markdown

No description provided.

@tresf

tresf commented Nov 8, 2023 •

Copy link
Copy Markdown

We can avoid changing the method signatures by wrapping the IOException. This will maintain the existing method signatures while providing consistent exception messages to the client.

    /** Exception occurred in native code **/
    final public static String TYPE_NATIVE_EXCEPTION = "Native exception occurred: %s";
    // ...
    public static SerialPortException wrapNativeException(Exception e, SerialPort port, String methodName) {
        return new SerialPortException(port, methodName, String.format(TYPE_NATIVE_EXCEPTION, e.getLocalizedMessage()));
    }
    public boolean writeBytes(byte[] buffer) throws SerialPortException {
        checkPortOpened("writeBytes()");
        boolean success;
        try {
            success = serialInterface.writeBytes(portHandle, buffer);
        } catch(IOException io) {
            throw SerialPortException.wrapNativeException(io, this, "writeBytes");
        }
        return success;
    }

@tresf
tresf self-requested a review November 8, 2023 18:32

@tresf tresf left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested per #146 (comment)

@hiddenalpha
hiddenalpha requested a review from tresf November 8, 2023 19:40
@tresf
tresf merged commit 4a8e4ac into java-native:master Nov 8, 2023
@hiddenalpha
hiddenalpha deleted the report-write-bytes-errors-as-exceptions-20231108 branch November 9, 2023 01:05
@pietrygamat pietrygamat added this to the 2.9.6 milestone Dec 1, 2023
@hiddenalpha
hiddenalpha restored the report-write-bytes-errors-as-exceptions-20231108 branch February 16, 2024 20:53
@hiddenalpha
hiddenalpha deleted the report-write-bytes-errors-as-exceptions-20231108 branch November 23, 2024 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants