-
Notifications
You must be signed in to change notification settings - Fork 89
fix(JsBridge): meaningful error when module/method routing fails (#25) #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,13 +118,17 @@ public void setValue(String value) { | |
| @NonNull | ||
| static JBArgumentParser parse(@Nullable String jsonString) { | ||
| JBArgumentParser parser = new JBArgumentParser(); | ||
| if(TextUtils.isEmpty(jsonString) || (!jsonString.startsWith("{") && | ||
| !jsonString.startsWith("["))) { | ||
| // Issue #25: trim before the startsWith check so a leading | ||
| // whitespace or BOM coming through the prompt channel does | ||
| // not look like a malformed payload. | ||
| String trimmed = jsonString == null ? null : jsonString.trim(); | ||
| if(TextUtils.isEmpty(trimmed) || (!trimmed.startsWith("{") && | ||
| !trimmed.startsWith("["))) { | ||
| parser.setThrowable(new JSONException("Argument error: " + jsonString)); | ||
| return parser; | ||
| } | ||
| try { | ||
| JSONObject jsonObject = new JSONObject(jsonString); | ||
| JSONObject jsonObject = new JSONObject(trimmed); | ||
|
Comment on lines
+125
to
+131
|
||
| parser.setId(jsonObject.optLong("id")); | ||
| parser.setMethod(jsonObject.optString("method")); | ||
| parser.setModule(jsonObject.optString("module")); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,10 +273,25 @@ private boolean onCallJsPrompt(String methodArgs, Object result) { | |
| if (argumentParser.isSuccess() && !TextUtils.isEmpty(argumentParser.getModule()) | ||
| && !TextUtils.isEmpty(argumentParser.getMethod())) { | ||
| JsModule findModule = getModule(argumentParser.getModule()); | ||
| if (findModule != null) { | ||
| HashMap<String, JsMethod> methodHashMap = exposedMethods.get(findModule); | ||
| if (methodHashMap != null && !methodHashMap.isEmpty() && methodHashMap.containsKey( | ||
| argumentParser.getMethod())) { | ||
| // Issue #25: report routing failures with the actual cause | ||
| // instead of a generic "JBArgument Parse error" which makes | ||
| // every developer think their JSON is malformed. | ||
| if (findModule == null) { | ||
| String msg = "Module not found: " + argumentParser.getModule(); | ||
| JBLog.e(msg, null); | ||
| setJsPromptResult(result, false, msg); | ||
| return true; | ||
| } | ||
| HashMap<String, JsMethod> methodHashMap = exposedMethods.get(findModule); | ||
| if (methodHashMap == null || methodHashMap.isEmpty() | ||
| || !methodHashMap.containsKey(argumentParser.getMethod())) { | ||
| String msg = "Method not found: " + argumentParser.getModule() | ||
| + "." + argumentParser.getMethod(); | ||
| JBLog.e(msg, null); | ||
| setJsPromptResult(result, false, msg); | ||
| return true; | ||
| } | ||
| if (true) { | ||
| JsMethod method = methodHashMap.get(argumentParser.getMethod()); | ||
| List<JBArgumentParser.Parameter> parameters = argumentParser.getParameters(); | ||
|
Comment on lines
+294
to
296
|
||
| int length = method.getParameterType().size(); | ||
|
|
@@ -327,9 +342,6 @@ private boolean onCallJsPrompt(String methodArgs, Object result) { | |
| } | ||
| return true; | ||
| } | ||
| } | ||
| setJsPromptResult(result, false, "JBArgument Parse error"); | ||
| return true; | ||
| } | ||
| JBLog.e("JBArgument error", argumentParser.getThrowable()); | ||
| setJsPromptResult(result, false, argumentParser.getErrorMsg()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says trimming handles a leading BOM, but
String.trim()does not remove the UTF-8 BOM character (\uFEFF). Either strip a BOM explicitly (and document it), or remove the BOM claim from the comment to avoid misleading future maintainers.