Skip to content

Commit

Permalink
Merge pull request #330 from Blues9527/dev_blues_error_msg_fix
Browse files Browse the repository at this point in the history
Optimize error messages
  • Loading branch information
wanbing authored Aug 15, 2023
2 parents 6848f28 + 82c4070 commit 798f4df
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 19 deletions.
20 changes: 19 additions & 1 deletion compiler/lib/src/post_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:crypto/crypto.dart' show md5;
import 'package:fair_compiler/src/state_transfer.dart';
import 'package:path/path.dart' as path;
import 'package:fair_dart2js/index.dart' as dart2js;
import 'package:yaml/yaml.dart';
import 'helper.dart' show FlatCompiler, ModuleNameHelper;

class ArchiveBuilder extends PostProcessBuilder with FlatCompiler {
Expand Down Expand Up @@ -56,7 +57,24 @@ class ArchiveBuilder extends PostProcessBuilder with FlatCompiler {
print('\u001b[33m [Fair Dart2JS] partPath => ${partPath} \u001b[0m');
if (File(partPath).existsSync()) {
try {
var result = await dart2js.convertFile(partPath, true);
var uglify = true;

var optionsYamlPath =
path.join(Directory.current.path, 'fair_compiler_options.yaml');

var optionYamlFile = File(optionsYamlPath);
if (optionYamlFile.existsSync()) {
var optionsYaml =
loadYaml(optionYamlFile.readAsStringSync()) as YamlMap?;

if (optionsYaml != null && optionsYaml.containsKey('uglify')) {
uglify = optionsYaml['uglify'];
}
}

print('\u001b[33m [Fair Dart2JS] uglify option: => $uglify \u001b[0m');

var result = await dart2js.convertFile(partPath, uglify);
File(jsName)..writeAsStringSync(result);
} catch (e) {
print('[Fair Dart2JS] e => ${e}');
Expand Down
1 change: 1 addition & 0 deletions compiler/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
glob: ^2.0.1
pedantic: ^1.11.1
http: ^0.13.3
yaml: ^3.0.0

fair_annotation: ^2.3.0
# fair_annotation:
Expand Down
15 changes: 15 additions & 0 deletions fair/android/src/main/java/com/wuba/fair/channel/FairFfi.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import androidx.annotation.Keep;

import org.json.JSONException;
import org.json.JSONObject;

/**
* dart ffi
*/
Expand Down Expand Up @@ -66,6 +69,18 @@ public void runTask() {
return result[0].toString();
}
}
try {
//when an exception occurs, return the function name
JSONObject jsonObject = new JSONObject(args);
if (jsonObject.has("args")) {
JSONObject funObject = new JSONObject(jsonObject.optString("args"));
if(funObject.has("funcName")){
return String.format("Runtime error while invoke JavaScript method:%s()", funObject.optString("funcName"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}

return "jsAppObj is null";
}
Expand Down
17 changes: 17 additions & 0 deletions fair/android/src/main/java/com/wuba/fair/core/FairV8JsLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Function;
import com.eclipsesource.v8.V8Object;
import com.eclipsesource.v8.V8ScriptCompilationException;
import com.wuba.fair.FairPlugin;
import com.wuba.fair.callback.JsResultCallback;
import com.wuba.fair.constant.Constant;
Expand Down Expand Up @@ -125,6 +126,22 @@ public void loadMainJs(Object arguments, JsResultCallback<String> callback) {
getV8JsExecutor().loadJS(jsName, jsLocalPath, callback);
} catch (Exception e) {
e.printStackTrace();
//avoid loading all the time
if (callback != null) {
try {
JSONObject errorResult = new JSONObject();
errorResult.put("status","error");
if (e instanceof V8ScriptCompilationException){
errorResult.put("errorInfo",e.toString());
errorResult.put("lineNumber",((V8ScriptCompilationException) e).getLineNumber());
}else{
errorResult.put("errorInfo",e.getLocalizedMessage());
}
callback.call(errorResult.toString());
} catch (JSONException ex) {
callback.call("result");
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public abstract class FairTask implements Runnable {

@Override
public void run() {
FairLogger.d("当前的线程名称" + Thread.currentThread());

runTask();

Expand Down
35 changes: 35 additions & 0 deletions fair/ios/Classes/FairDynamicJSPlugin/FairDartBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ - (void)setDartListener {
if ([method isEqualToString:@"loadMainJs"]) {
if ([strongSelf.delegate respondsToSelector:@selector(injectionJSScriptWtihJSScript: callback:)]) {
[strongSelf.delegate injectionJSScriptWtihJSScript:model.path callback:^(id result, NSError *error) {
JSValue *value = result;
if (value && [value isKindOfClass:[JSValue class]]) {
NSString *str = value.toString;
if([str isEqualToString:@"undefined"]){
NSMutableDictionary *result=[NSMutableDictionary dictionary];
result[@"status"] = @"error";
result[@"errorInfo"]=@"load JavaScript error";
result[@"lineNumber"]=@-1;

NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


callback(jsonStr);
return;
}
}
callback(@"success");
}];
}
Expand Down Expand Up @@ -163,6 +181,23 @@ - (const char *)executeScriptSyncImpl:(char *)args
}
NSString *result = [NSString stringWithFormat:@"%@", obj.toString];
FairLog(@"result:%@", result);
if([result isEqualToString:@"undefined"]){
//取args中的funcName字段
//arg ===> "{\"pageName\":\"null#0\",\"type\":\"method\",\"args\":{\"funcName\":\"_getAuth\",\"args\":null}}"
NSString *str = [NSString stringWithUTF8String:args];
NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

NSDictionary *args = dic[@"args"];
NSString *funcName = args[@"funcName"];

FairLog(@"invoke funcName:%@",funcName);

NSString *errorResult = [NSString stringWithFormat:@"Runtime error while invoke JavaScript method:%@()", funcName];

return errorResult.UTF8String;
}
return result.UTF8String;
}

Expand Down
19 changes: 17 additions & 2 deletions fair/lib/src/internal/bind_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ class BindingData {
} else {
result = _functions?['runtimeInvokeMethodSync']?.call(funcName);
}
var value = jsonDecode(result);
return value['result']['result'];
try {
var value = jsonDecode(result);
return value['result']['result'];
} catch (e) {
throw RuntimeError(errorMsg: result);
}
} else {
return _functions?[funcName]?.call();
}
Expand Down Expand Up @@ -158,3 +162,14 @@ class BindingData {
_functions?.clear();
}
}

class RuntimeError extends Error {
final String errorMsg;

RuntimeError({required this.errorMsg});

@override
String toString() {
return errorMsg;
}
}
30 changes: 28 additions & 2 deletions fair/lib/src/internal/error_tips.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* found in the LICENSE file.
*/

import 'package:fair/src/internal/stack_trace_detail.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'warning_dialog_widget.dart';

Expand All @@ -15,9 +15,21 @@ class WarningWidget extends StatelessWidget {
final String? solution;
final dynamic error;
final BuildContext? parentContext;
final String? stackTrace;
final Map? errorBlock;
final List<int>? highlightLines;


const WarningWidget({Key? key, this.name, this.url, this.error, this.solution,this.parentContext})
const WarningWidget(
{Key? key,
this.name,
this.url,
this.error,
this.solution,
this.parentContext,
this.stackTrace,
this.errorBlock,
this.highlightLines})
: super(key: key);

@override
Expand Down Expand Up @@ -65,6 +77,20 @@ class WarningWidget extends StatelessWidget {
cancelFun: (){
Navigator.pop(parentContext!);
},
stackTraceVisible: stackTrace != null,
viewStackTrace: () {
Navigator.push(
parentContext!,
MaterialPageRoute(
builder: (context) =>
StackTraceDetailPage(
stackTrace: stackTrace!,
name: name,
errorJson: errorBlock,
error: error,
highlightLines: highlightLines,
)));
},
);
}
);
Expand Down
Loading

0 comments on commit 798f4df

Please sign in to comment.