Skip to content

Commit

Permalink
chore(java): new error system
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaarf committed Sep 5, 2024
1 parent 921a8ee commit cd8b0c6
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 130 deletions.
18 changes: 9 additions & 9 deletions dist/java/src/mp/code/BufferController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import mp.code.data.Cursor;
import mp.code.data.TextChange;
import mp.code.exceptions.CodeMPException;
import mp.code.exceptions.ControllerException;

import java.util.Optional;

Expand All @@ -18,23 +18,23 @@ public String getName() {
return get_name(this.ptr);
}

private static native String get_content(long self) throws CodeMPException;
public String getContent() throws CodeMPException {
private static native String get_content(long self) throws ControllerException;
public String getContent() throws ControllerException {
return get_content(this.ptr);
}

private static native TextChange try_recv(long self) throws CodeMPException;
public Optional<TextChange> tryRecv() throws CodeMPException {
private static native TextChange try_recv(long self) throws ControllerException;
public Optional<TextChange> tryRecv() throws ControllerException {
return Optional.ofNullable(try_recv(this.ptr));
}

private static native Cursor recv(long self) throws CodeMPException;
public Cursor recv() throws CodeMPException {
private static native Cursor recv(long self) throws ControllerException;
public Cursor recv() throws ControllerException {
return recv(this.ptr);
}

private static native void send(long self, TextChange change) throws CodeMPException;
public void send(TextChange change) throws CodeMPException {
private static native void send(long self, TextChange change) throws ControllerException;
public void send(TextChange change) throws ControllerException {
send(this.ptr, change);
}

Expand Down
37 changes: 19 additions & 18 deletions dist/java/src/mp/code/Client.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package mp.code;

import cz.adamh.utils.NativeUtils;
import mp.code.exceptions.CodeMPException;
import mp.code.exceptions.ConnectionException;
import mp.code.exceptions.ConnectionRemoteException;

import java.io.IOException;
import java.util.Optional;

public class Client {
private final long ptr;

public static native Client connect(String url, String username, String password) throws CodeMPException;
public static native Client connect(String url, String username, String password) throws ConnectionException;
Client(long ptr) {
this.ptr = ptr;
}
Expand All @@ -19,28 +20,28 @@ public String getUrl() {
return get_url(this.ptr);
}

private static native Workspace join_workspace(long self, String id) throws CodeMPException;
public Workspace joinWorkspace(String id) throws CodeMPException {
private static native Workspace join_workspace(long self, String id) throws ConnectionException;
public Workspace joinWorkspace(String id) throws ConnectionException {
return join_workspace(this.ptr, id);
}

private static native void create_workspace(long self, String id) throws CodeMPException;
public void createWorkspace(String id) throws CodeMPException {
return create_workspace(this.ptr, id);
private static native void create_workspace(long self, String id) throws ConnectionRemoteException;
public void createWorkspace(String id) throws ConnectionRemoteException {
create_workspace(this.ptr, id);
}

private static native void delete_workspace(long self, String id) throws CodeMPException;
public void deleteWorkspace(String id) throws CodeMPException {
return delete_workspace(this.ptr, id);
private static native void delete_workspace(long self, String id) throws ConnectionRemoteException;
public void deleteWorkspace(String id) throws ConnectionRemoteException {
delete_workspace(this.ptr, id);
}

private static native void invite_to_workspace(long self, String ws, String usr) throws CodeMPException;
public void inviteToWorkspace(String ws, String usr) throws CodeMPException {
return invite_to_workspace(this.ptr, id);
private static native void invite_to_workspace(long self, String ws, String usr) throws ConnectionRemoteException;
public void inviteToWorkspace(String ws, String usr) throws ConnectionRemoteException {
invite_to_workspace(this.ptr, ws, usr);
}

private static native String[] list_workspaces(long self, boolean owned, boolean invited) throws CodeMPException;
public String[] listWorkspaces(boolean owned, boolean invited) throws CodeMPException {
private static native String[] list_workspaces(long self, boolean owned, boolean invited) throws ConnectionRemoteException;
public String[] listWorkspaces(boolean owned, boolean invited) throws ConnectionRemoteException {
return list_workspaces(this.ptr, owned, invited);
}

Expand All @@ -54,9 +55,9 @@ public Optional<Workspace> getWorkspace() {
return Optional.ofNullable(get_workspace(this.ptr));
}

private static native void refresh_native(long self);
public void refresh() {
return refresh_native(this.ptr);
private static native void refresh_native(long self) throws ConnectionRemoteException;
public void refresh() throws ConnectionRemoteException {
refresh_native(this.ptr);
}

private static native void free(long self);
Expand Down
14 changes: 7 additions & 7 deletions dist/java/src/mp/code/CursorController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mp.code;

import mp.code.data.Cursor;
import mp.code.exceptions.CodeMPException;
import mp.code.exceptions.ControllerException;

import java.util.Optional;

Expand All @@ -12,18 +12,18 @@ public class CursorController {
this.ptr = ptr;
}

private static native Cursor try_recv(long self) throws CodeMPException;
public Optional<Cursor> tryRecv() throws CodeMPException {
private static native Cursor try_recv(long self) throws ControllerException;
public Optional<Cursor> tryRecv() throws ControllerException {
return Optional.ofNullable(try_recv(this.ptr));
}

private static native Cursor recv(long self) throws CodeMPException;
public Cursor recv() throws CodeMPException {
private static native Cursor recv(long self) throws ControllerException;
public Cursor recv() throws ControllerException {
return recv(this.ptr);
}

private static native void send(long self, Cursor cursor) throws CodeMPException;
public void send(Cursor cursor) throws CodeMPException {
private static native void send(long self, Cursor cursor) throws ControllerException;
public void send(Cursor cursor) throws ControllerException {
send(this.ptr, cursor);
}

Expand Down
36 changes: 19 additions & 17 deletions dist/java/src/mp/code/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.UUID;

import mp.code.data.DetachResult;
import mp.code.exceptions.CodeMPException;
import mp.code.exceptions.ConnectionException;
import mp.code.exceptions.ConnectionRemoteException;
import mp.code.exceptions.ControllerException;

public class Workspace {
private final long ptr;
Expand Down Expand Up @@ -33,13 +35,13 @@ public String[] getFileTree(Optional<String> filter) {
return get_file_tree(this.ptr, filter.orElse(null));
}

private static native void create_buffer(String path) throws CodeMPException;
public void createBuffer(String path) throws CodeMPException {
private static native void create_buffer(String path) throws ConnectionRemoteException;
public void createBuffer(String path) throws ConnectionRemoteException {
create_buffer(path);
}

private static native BufferController attach_to_buffer(long self, String path) throws CodeMPException;
public BufferController attachToBuffer(String path) throws CodeMPException {
private static native BufferController attach_to_buffer(long self, String path) throws ConnectionException;
public BufferController attachToBuffer(String path) throws ConnectionException {
return attach_to_buffer(ptr, path);
}

Expand All @@ -48,33 +50,33 @@ public DetachResult detachFromBuffer(String path) {
return detach_from_buffer(this.ptr, path);
}

private static native void fetch_buffers(long self) throws CodeMPException;
public void fetchBuffers() throws CodeMPException {
private static native void fetch_buffers(long self) throws ConnectionRemoteException;
public void fetchBuffers() throws ConnectionRemoteException {
fetch_buffers(this.ptr);
}

private static native void fetch_users(long self) throws CodeMPException;
public void fetchUsers() throws CodeMPException {
private static native void fetch_users(long self) throws ConnectionRemoteException;
public void fetchUsers() throws ConnectionRemoteException {
fetch_buffers(this.ptr);
}

private static native UUID[] list_buffer_users(long self, String path) throws CodeMPException;
public UUID[] listBufferUsers(String path) throws CodeMPException {
private static native UUID[] list_buffer_users(long self, String path) throws ConnectionRemoteException;
public UUID[] listBufferUsers(String path) throws ConnectionRemoteException {
return list_buffer_users(this.ptr, path);
}

private static native void delete_buffer(long self, String path) throws CodeMPException;
public void deleteBuffer(String path) throws CodeMPException {
private static native void delete_buffer(long self, String path) throws ConnectionRemoteException;
public void deleteBuffer(String path) throws ConnectionRemoteException {
delete_buffer(this.ptr, path);
}

private static native Event event(long self) throws CodeMPException;
public Event event() throws CodeMPException {
private static native Event event(long self) throws ControllerException;
public Event event() throws ControllerException {
return event(this.ptr);
}

private static native BufferController select_buffer(long self, long timeout) throws CodeMPException;
public Optional<BufferController> selectBuffer(long timeout) throws CodeMPException {
private static native BufferController select_buffer(long self, long timeout) throws ControllerException;
public Optional<BufferController> selectBuffer(long timeout) throws ControllerException {
return Optional.ofNullable(select_buffer(this.ptr, timeout));
}

Expand Down
7 changes: 0 additions & 7 deletions dist/java/src/mp/code/exceptions/ChannelException.java

This file was deleted.

10 changes: 0 additions & 10 deletions dist/java/src/mp/code/exceptions/CodeMPException.java

This file was deleted.

10 changes: 10 additions & 0 deletions dist/java/src/mp/code/exceptions/ConnectionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mp.code.exceptions;

/**
* An exception that may occur when processing network requests.
*/
public abstract class ConnectionException extends Exception {
protected ConnectionException(String msg) {
super(msg);
}
}
10 changes: 10 additions & 0 deletions dist/java/src/mp/code/exceptions/ConnectionRemoteException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mp.code.exceptions;

/**
* An exception returned by the server as a response.
*/
public abstract class ConnectionRemoteException extends Exception {
protected ConnectionRemoteException(String msg) {
super(msg);
}
}
10 changes: 10 additions & 0 deletions dist/java/src/mp/code/exceptions/ConnectionTransportException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mp.code.exceptions;

/**
* An exception that occurred from the underlying tonic layer.
*/
public abstract class ConnectionTransportException extends Exception {
protected ConnectionTransportException(String msg) {
super(msg);
}
}
13 changes: 13 additions & 0 deletions dist/java/src/mp/code/exceptions/ControllerException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mp.code.exceptions;

/**
* An exception that may occur when a {@link mp.code.BufferController} or
* a {@link mp.code.CursorController} perform an illegal operation.
* It may also occur as a result of {@link mp.code.Workspace#event()} and
* {@link mp.code.Workspace#selectBuffer(long)}.
*/
public abstract class ControllerException extends Exception {
protected ControllerException(String msg) {
super(msg);
}
}
11 changes: 11 additions & 0 deletions dist/java/src/mp/code/exceptions/ControllerStoppedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mp.code.exceptions;

/**
* An exception that occurs when attempting to send an operation when
* the worker has already stopped.
*/
public class ControllerStoppedException extends ControllerException {
protected ControllerStoppedException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mp.code.exceptions;

/**
* An exception that occurs when the underlying controller stopped before
* fulfilling the request, without rejecting it first.
*/
public class ControllerUnfulfilledException extends ControllerException {
protected ControllerUnfulfilledException(String msg) {
super(msg);
}
}
7 changes: 0 additions & 7 deletions dist/java/src/mp/code/exceptions/DeadlockedException.java

This file was deleted.

7 changes: 0 additions & 7 deletions dist/java/src/mp/code/exceptions/InvalidStateException.java

This file was deleted.

7 changes: 0 additions & 7 deletions dist/java/src/mp/code/exceptions/TransportException.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/ffi/java/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub extern "system" fn Java_mp_code_BufferController_get_1name(
self_ptr: jlong,
) -> jstring {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
let content = controller.name();
let content = controller.path();
env.new_string(content)
.jexcept(&mut env)
.as_raw()
Expand Down
Loading

0 comments on commit cd8b0c6

Please sign in to comment.