001package Torello.Browser; 002 003import javax.json.JsonObject; 004import java.util.function.Consumer; 005 006// <LI> {@link #closeSocket(WebSocketSender)} — forcibly closes connection 007/** 008 * RDPError represents a fatal or protocol-level error encountered while communicating with 009 * a Chrome Debugging Protocol WebSocket. 010 * 011 * <BR /><BR /> 012 * This class also defines several standard error-handling behaviors (as static constants 013 * or factories) that may be passed to the {@link WebSocketSender} constructor as a 014 * {@code Consumer<RDPError>}. 015 */ 016public final class RDPError 017{ 018 // ******************************************************************************************** 019 // ******************************************************************************************** 020 // Public, Final Fields 021 // ******************************************************************************************** 022 // ******************************************************************************************** 023 024 025 public final String message; 026 public final Throwable cause; 027 public final JsonObject context; 028 029 030 // ******************************************************************************************** 031 // ******************************************************************************************** 032 // Constructor 033 // ******************************************************************************************** 034 // ******************************************************************************************** 035 036 037 RDPError(String message) 038 { 039 if (message == null) System.err.println("Input to Error has a null message"); 040 041 this.message = message; 042 this.context = null; 043 this.cause = null; 044 } 045 046 RDPError(final String message, final JsonObject jo) 047 { 048 if (message == null) 049 System.err.println("Input to RDPError Constructor has a null message"); 050 051 if (jo == null) System.err.println 052 ("Input to RDPError Constructor has a null JsonObject-Parameter"); 053 054 this.message = message; 055 this.context = jo; 056 this.cause = null; 057 } 058 059 RDPError(final String message, final Throwable cause) 060 { 061 if (message == null) System.err.println 062 ("Input to RDPError Constructor has a null message"); 063 064 if (cause == null) System.err.println 065 ("Input to RDPError Constructor has a null Throwable-Parameter"); 066 067 this.message = message; 068 this.cause = cause; 069 this.context = null; 070 } 071 072 RDPError(String message, JsonObject jo, Throwable cause) 073 { 074 if (message == null) System.err.println 075 ("Input to RDPError Constructor has a null message"); 076 077 if (jo == null) System.err.println 078 ("Input to RDPError Constructor has a null JsonObject-Parameter"); 079 080 if (cause == null) System.err.println 081 ("Input to RDPError Constructor has a null Throwable-Parameter"); 082 083 this.message = message; 084 this.context = jo; 085 this.cause = cause; 086 } 087 088 089 // ******************************************************************************************** 090 // ******************************************************************************************** 091 // Instance Methods 092 // ******************************************************************************************** 093 // ******************************************************************************************** 094 095 096 @Override 097 public String toString() 098 { 099 return 100 "{\n" + 101 message + 102 ((cause != null) ? ("\nCause: " + cause) : "") + 103 ((context != null) ? ("\nJsonObject Context:\n" + context.toString()) : "") + 104 '}'; 105 } 106 107 108 // ******************************************************************************************** 109 // ******************************************************************************************** 110 // Predefined Error Handlers 111 // ******************************************************************************************** 112 // ******************************************************************************************** 113 114 115 public static final Consumer<RDPError> EXIT_ON_ERROR = (RDPError error) -> 116 { System.exit(1); }; 117 118 public static final Consumer<RDPError> THROW_INTERNAL_ERROR = (RDPError error) -> 119 { 120 throw new InternalError 121 ("[RDPError.THROW_INTERNAL_ERROR] " + error.message, error.cause); 122 }; 123 124 /* 125 public static Consumer<RDPError> closeSocket(WebSocketSender sender) 126 { 127 return error -> { 128 System.err.println("[RDPError.closeSocket] " + error); 129 sender.webSocket().sendClose(1011, "Internal Error"); 130 }; 131 } 132 */ 133 134 public static final Consumer<RDPError> NO_OP = (RDPError error) -> { }; 135}