Flutter Engine
The Flutter Engine
RPCError.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package org.dartlang.vm.service.element;
15
16import com.google.gson.JsonElement;
17import com.google.gson.JsonObject;
18import org.dartlang.vm.service.internal.VmServiceConst;
19
20/**
21 * When an RPC encounters an error, it is provided in the _error_ property of the response object.
22 * JSON-RPC errors always provide _code_, _message_, and _data_ properties. <br/>
23 * Here is an example error response for our [streamListen](#streamlisten) request above. This error
24 * would be generated if we were attempting to subscribe to the _GC_ stream multiple times from the
25 * same client.
26 *
27 * <pre>
28 * {
29 * "jsonrpc": "2.0",
30 * "error": {
31 * "code": 103,
32 * "message": "Stream already subscribed",
33 * "data": {
34 * "details": "The stream 'GC' is already subscribed"
35 * }
36 * }
37 * "id": "2"
38 * }
39 * </pre>
40 * <p>
41 * In addition the [error codes](http://www.jsonrpc.org/specification#error_object) specified in
42 * the JSON-RPC spec, we use the following application specific error codes:
43 *
44 * <pre>
45 * code | message | meaning
46 * ---- | ------- | -------
47 * 100 | Feature is disabled | The operation is unable to complete because a feature is disabled
48 * 101 | VM must be paused | This operation is only valid when the VM is paused
49 * 102 | Cannot add breakpoint | The VM is unable to add a breakpoint at the specified line or function
50 * 103 | Stream already subscribed | The client is already subscribed to the specified _streamId_
51 * 104 | Stream not subscribed | The client is not subscribed to the specified _streamId_
52 * </pre>
53 */
54public class RPCError extends Element implements VmServiceConst {
55
56 /**
57 * The response code used by the client when it receives a response from the server that it did
58 * not expect. For example, it requested a library element but received a list.
59 */
60 public static final int UNEXPECTED_RESPONSE = 5;
61
62 public static RPCError unexpected(String expectedType, Response response) {
63 String errMsg = "Expected type " + expectedType + " but received " + response.getType();
64 if (response instanceof Sentinel) {
65 errMsg += ": " + ((Sentinel) response).getKind();
66 }
67 JsonObject json = new JsonObject();
68 json.addProperty("code", UNEXPECTED_RESPONSE);
69 json.addProperty("message", errMsg);
70 JsonObject data = new JsonObject();
71 data.addProperty("details", errMsg);
72 data.add("response", response.getJson());
73 json.add("data", data);
74 return new RPCError(json);
75 }
76
77 public RPCError(JsonObject json) {
78 super(json);
79 }
80
81 public int getCode() {
82 return json.get("code").getAsInt();
83 }
84
85 public String getDetails() {
86 JsonElement data = json.get("data");
87 if (data instanceof JsonObject) {
88 JsonElement details = ((JsonObject) data).get("details");
89 if (details != null) {
90 return details.getAsString();
91 }
92 }
93 return null;
94 }
95
96 public String getMessage() {
97 return json.get("message").getAsString();
98 }
99
100 public JsonObject getRequest() {
101 JsonElement data = json.get("data");
102 if (data instanceof JsonObject) {
103 JsonElement request = ((JsonObject) data).get("request");
104 if (request instanceof JsonObject) {
105 return (JsonObject) request;
106 }
107 }
108 return null;
109 }
110}
static RPCError unexpected(String expectedType, Response response)
Definition: RPCError.java:62
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63