Skip to content

Commit

Permalink
Merge pull request #6992 from troizet/php_exception_breakpoint_show_m…
Browse files Browse the repository at this point in the history
…essage

PHP: Implemented display of exception message when exception breakpoints are hit
  • Loading branch information
junichi11 committed Feb 4, 2024
2 parents 35a83e0 + da5a813 commit b33890d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

import java.util.Map;
import java.util.WeakHashMap;
import org.netbeans.api.annotations.common.NullAllowed;
import org.netbeans.api.debugger.Breakpoint;
import org.netbeans.api.debugger.Breakpoint.VALIDITY;
import org.netbeans.api.debugger.DebuggerManager;
import org.netbeans.modules.php.api.util.StringUtils;
import org.netbeans.modules.php.dbgp.DebugSession;
import org.netbeans.modules.php.dbgp.models.ViewModelSupport;
import org.netbeans.modules.php.dbgp.packets.Stack;
Expand Down Expand Up @@ -53,6 +54,12 @@ public class BreakpointModel extends ViewModelSupport implements NodeModel {
private static final String METHOD = "TXT_Method"; // NOI18N
private static final String EXCEPTION = "TXT_Exception"; // NOI18N
private static final String PARENS = "()"; // NOI18N
private static final String MESSAGE = "Message: "; // NOI18N
private static final String CODE = "Code: "; // NOI18N
private static final String FONT_COLOR = "<font color=\"#7D694A\">"; //NOI18N
private static final String CLOSE_FONT = "</font>"; //NOI18N
private static final String OPEN_HTML = "<html>"; //NOI18N
private static final String CLOSE_HTML = "</html>"; //NOI18N
private final Map<DebugSession, AbstractBreakpoint> myCurrentBreakpoints;
private volatile boolean searchCurrentBreakpointById = false;

Expand All @@ -79,14 +86,37 @@ public String getDisplayName(Object node) throws UnknownTypeException {
return builder.toString();
} else if (node instanceof ExceptionBreakpoint) {
ExceptionBreakpoint breakpoint = (ExceptionBreakpoint) node;
StringBuilder builder = new StringBuilder(NbBundle.getMessage(BreakpointModel.class, EXCEPTION));
builder.append(" "); // NOI18N
builder.append(breakpoint.getException());
StringBuilder builder = new StringBuilder()
.append(OPEN_HTML)
.append(NbBundle.getMessage(BreakpointModel.class, EXCEPTION))
.append(" ") // NOI18N
.append(breakpoint.getException());
String message = breakpoint.getExceptionMessage();
String code = breakpoint.getExceptionCode();
synchronized (myCurrentBreakpoints) {
for (AbstractBreakpoint brkp : myCurrentBreakpoints.values()) {
if (breakpoint.equals(brkp)) {
buildAppend(builder, MESSAGE, message);
buildAppend(builder, CODE, code);
}
}
}
builder.append(CLOSE_HTML);
return builder.toString();
}
throw new UnknownTypeException(node);
}

private void buildAppend(StringBuilder builder, String prepend, @NullAllowed String text) {
if (!StringUtils.isEmpty(text)) {
builder.append(" ") // NOI18N
.append(FONT_COLOR)
.append(prepend)
.append(text)
.append(CLOSE_FONT);
}
}

@Override
public String getIconBase(Object node) throws UnknownTypeException {
synchronized (myCurrentBreakpoints) {
Expand Down Expand Up @@ -221,6 +251,12 @@ private void updateCurrentBreakpoint(DebugSession session, Breakpoint breakpoint
}
}

public AbstractBreakpoint getCurrentBreakpoint(DebugSession session) {
synchronized (myCurrentBreakpoints) {
return myCurrentBreakpoints.get(session);
}
}

public void setSearchCurrentBreakpointById(boolean flag) {
searchCurrentBreakpointById = flag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.netbeans.modules.php.dbgp.breakpoints;

import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.annotations.common.NullAllowed;
import org.netbeans.modules.php.api.util.StringUtils;
import org.netbeans.modules.php.dbgp.DebugSession;

/**
Expand All @@ -26,7 +29,13 @@
*
*/
public class ExceptionBreakpoint extends AbstractBreakpoint {
private static final String FONT_GRAY_COLOR = "<font color=\"#999999\">"; //NOI18N
private static final String CLOSE_FONT = "</font>"; //NOI18N
private final String exceptionName;
@NullAllowed
private volatile String message;
@NullAllowed
private volatile String code;

public ExceptionBreakpoint(String exceptionName) {
this.exceptionName = exceptionName;
Expand All @@ -36,6 +45,37 @@ public String getException() {
return exceptionName;
}

public void setExceptionMessage(String message) {
this.message = message;
}

@CheckForNull
public String getExceptionMessage() {
return buildText(message);
}

public void setExceptionCode(String code) {
this.code = code;
}

@CheckForNull
public String getExceptionCode() {
return buildText(code);
}

@CheckForNull
private String buildText(String text) {
if (!StringUtils.isEmpty(text)) {
StringBuilder builder = new StringBuilder()
.append(FONT_GRAY_COLOR)
.append(text)
.append(CLOSE_FONT);
return builder.toString();
}

return null;
}

@Override
public boolean isSessionRelated(DebugSession session) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
package org.netbeans.modules.php.dbgp.packets;

import org.netbeans.modules.php.dbgp.DebugSession;
import org.netbeans.modules.php.dbgp.breakpoints.AbstractBreakpoint;
import org.netbeans.modules.php.dbgp.breakpoints.BreakpointModel;
import org.netbeans.modules.php.dbgp.breakpoints.ExceptionBreakpoint;
import org.w3c.dom.Node;

public class RunResponse extends StatusResponse {
private static final String BREAKPOINT = "breakpoint"; //NOI18N
private static final String BREAKPOINT_ID = "id"; //NOI18N
private static final String MESSAGE = "xdebug:message"; //NOI18N
private static final String CODE = "code"; //NOI18N

RunResponse(Node node) {
super(node);
Expand All @@ -38,16 +42,25 @@ public void process(DebugSession dbgSession, DbgpCommand command) {
dbgSession.processStatus(status, reason, command);
}

Node message = getChild(getNode(), MESSAGE);
String code = null;
if (message != null) {
code = getAttribute(message, CODE);
}

Node breakpoint = getChild(getNode(), BREAKPOINT);
if (breakpoint != null) {
String id = DbgpMessage.getAttribute(breakpoint, BREAKPOINT_ID);
if (id != null) {
updateBreakpointsView(dbgSession, id);
setCurrentBreakpoint(dbgSession, id);
if (message != null) {
setCurrentBreakpointText(dbgSession, message.getTextContent(), code);
}
}
}
}

private void updateBreakpointsView(DebugSession session, String id) {
private void setCurrentBreakpoint(DebugSession session, String id) {
DebugSession.IDESessionBridge bridge = session.getBridge();
if (bridge != null) {
BreakpointModel breakpointModel = bridge.getBreakpointModel();
Expand All @@ -57,4 +70,18 @@ private void updateBreakpointsView(DebugSession session, String id) {
}
}

private void setCurrentBreakpointText(DebugSession session, String message, String code) {
DebugSession.IDESessionBridge bridge = session.getBridge();
if (bridge != null) {
BreakpointModel breakpointModel = bridge.getBreakpointModel();
if (breakpointModel != null && breakpointModel.isSearchCurrentBreakpointById()) {
AbstractBreakpoint bp = breakpointModel.getCurrentBreakpoint(session);
if (bp instanceof ExceptionBreakpoint) {
((ExceptionBreakpoint) bp).setExceptionMessage(message);
((ExceptionBreakpoint) bp).setExceptionCode(code);
}
}
}
}

}

0 comments on commit b33890d

Please sign in to comment.