Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public class DebugException extends Exception {
private static final long serialVersionUID = 1L;
private int errorCode;

public DebugException() {
super();
Expand All @@ -29,4 +30,23 @@ public DebugException(String message, Throwable cause) {
public DebugException(Throwable cause) {
super(cause);
}

public DebugException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
}

public DebugException(String message, Throwable cause, int errorCode) {
super(message, cause);
this.errorCode = errorCode;
}

public DebugException(Throwable cause, int errorCode) {
super(cause);
this.errorCode = errorCode;
}

public int getErrorCode() {
return this.errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void detach() {

@Override
public void terminate() {
if (vm.process().isAlive()) {
if (vm.process() == null || vm.process().isAlive()) {
vm.exit(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class DebugAdapterContext implements IDebugAdapterContext {
private boolean debuggerPathsAreUri = true;
private boolean clientLinesStartAt1 = true;
private boolean clientPathsAreUri = false;
private boolean supportsRunInTerminalRequest;
private boolean isAttached = false;
private String[] sourcePaths;
private Charset debuggeeEncoding;
Expand Down Expand Up @@ -107,6 +108,16 @@ public void setClientPathsAreUri(boolean clientPathsAreUri) {
this.clientPathsAreUri = clientPathsAreUri;
}

@Override
public void setSupportsRunInTerminalRequest(boolean supportsRunInTerminalRequest) {
this.supportsRunInTerminalRequest = supportsRunInTerminalRequest;
}

@Override
public boolean supportsRunInTerminalRequest() {
return supportsRunInTerminalRequest;
}

@Override
public boolean isAttached() {
return isAttached;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package com.microsoft.java.debug.core.adapter;

import java.util.Arrays;

public enum ErrorCode {
UNKNOWN_FAILURE(1000),
UNRECOGNIZED_REQUEST_FAILURE(1001),
Expand All @@ -25,7 +27,8 @@ public enum ErrorCode {
EVALUATE_FAILURE(1010),
EMPTY_DEBUG_SESSION(1011),
INVALID_ENCODING(1012),
VM_TERMINATED(1013);
VM_TERMINATED(1013),
LAUNCH_IN_TERMINAL_FAILURE(1014);

private int id;

Expand All @@ -36,4 +39,22 @@ public enum ErrorCode {
public int getId() {
return id;
}

/**
* Get the corresponding ErrorCode type by the error code id.
* If the error code is not defined in the enum type, return ErrorCode.UNKNOWN_FAILURE.
* @param id
* the error code id.
* @return the ErrorCode type.
*/
public static ErrorCode parse(int id) {
ErrorCode[] found = Arrays.stream(ErrorCode.values()).filter(code -> {
return code.getId() == id;
}).toArray(ErrorCode[]::new);

if (found.length > 0) {
return found[0];
}
return ErrorCode.UNKNOWN_FAILURE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public interface IDebugAdapterContext {

void setClientPathsAreUri(boolean clientPathsAreUri);

void setSupportsRunInTerminalRequest(boolean supportsRunInTerminalRequest);

boolean supportsRunInTerminalRequest();

boolean isAttached();

void setAttached(boolean attached);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.DebugException;
import com.microsoft.java.debug.core.UsageDataSession;
import com.microsoft.java.debug.core.protocol.AbstractProtocolServer;
import com.microsoft.java.debug.core.protocol.Messages;
import com.sun.jdi.VMDisconnectedException;

public class ProtocolServer extends AbstractProtocolServer {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
Expand Down Expand Up @@ -74,20 +77,37 @@ public CompletableFuture<Messages.Response> sendRequest(Messages.Request request
@Override
protected void dispatchRequest(Messages.Request request) {
usageDataSession.recordRequest(request);
this.debugAdapter.dispatchRequest(request).whenComplete((response, ex) -> {
this.debugAdapter.dispatchRequest(request).thenCompose((response) -> {
CompletableFuture<Void> future = new CompletableFuture<>();
if (response != null) {
sendResponse(response);
} else if (ex != null) {
logger.log(Level.SEVERE, String.format("DebugSession dispatch exception: %s", ex.toString()), ex);
sendResponse(AdapterUtils.setErrorResponse(response,
ErrorCode.UNKNOWN_FAILURE,
ex.getMessage() != null ? ex.getMessage() : ex.toString()));
future.complete(null);
} else {
logger.log(Level.SEVERE, "The request dispatcher should not return null response.");
future.completeExceptionally(new DebugException("The request dispatcher should not return null response.",
ErrorCode.UNKNOWN_FAILURE.getId()));
}
return future;
}).exceptionally((ex) -> {
Messages.Response response = new Messages.Response(request.seq, request.command);
if (ex instanceof CompletionException && ex.getCause() != null) {
ex = ex.getCause();
}

if (ex instanceof VMDisconnectedException) {
// mark it success to avoid reporting error on VSCode.
response.success = true;
sendResponse(response);
} else if (ex instanceof DebugException) {
sendResponse(AdapterUtils.setErrorResponse(response,
ErrorCode.parse(((DebugException) ex).getErrorCode()),
ex.getMessage() != null ? ex.getMessage() : ex.toString()));
} else {
sendResponse(AdapterUtils.setErrorResponse(response,
ErrorCode.UNKNOWN_FAILURE,
"The request dispatcher should not return null response."));
ErrorCode.UNKNOWN_FAILURE,
ex.getMessage() != null ? ex.getMessage() : ex.toString()));
}
return null;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public CompletableFuture<Messages.Response> handle(Requests.Command command, Req
context.setClientPathsAreUri(false);
}
}
context.setSupportsRunInTerminalRequest(initializeArguments.supportsRunInTerminalRequest);

Types.Capabilities caps = new Types.Capabilities();
caps.supportsConfigurationDoneRequest = true;
Expand Down
Loading