tryCatch function

Future tryCatch(
  1. dynamic callback, {
  2. List? args,
  3. bool verbose = false,
  4. dynamic onError,
})

Implementation

Future<dynamic> tryCatch(dynamic callback,
	{List<dynamic>? args, bool verbose=false, dynamic onError}) async {
	Completer completer = Completer<dynamic>();

	var results;
	try {
		if(callback is Future){
			results = await callback;
		}

		if(callback is Function){
			if(args != null){
				results = callback(args);
			} else {
				results = callback();
			}
		}
	} catch(e){
		if(verbose){
			pretifyOutput('[TRY CATCH] ${e.toString()}', color: AqColor.red);
		}

		if(onError != null){
			if(onError is Future){
				await onError;
			}

			if(onError is Function){
				onError();
			}
		}
	}

	completer.complete(results);
	return completer.future;
}