// Searches for a TypedRpcException in an exception chain. internal static bool FindInChain(Exception exception, out TypedRpcException typedRpcException) { // Initializations typedRpcException = null; // Looks for TypedRpc Exceptions. while (exception != null && !(exception is TypedRpcException)) { // Keeps searching. exception = exception.InnerException; } return(typedRpcException != null); }
// Handles requests. public virtual async Task <JsonResponse> InvokeMethod(IOwinContext context) { // Declarations string data; JsonError error; JsonRequest jRequest; JsonResponse jResponse; object handler; MethodInfo methodInfo; object[] parameters; object result; // Initializations data = null; jRequest = null; try { // Retrieves method. error = GetMethod(context, out data, out jRequest, out handler, out methodInfo, out parameters); // Checks for any error. if (error != null) { return(MountError(jRequest == null ? null : jRequest.Id, error)); } // Invokes method. result = InvokeMethod(handler, methodInfo, parameters); // Checks if result is async. if (result is Task) { // Awaits method. await(Task) result; // Retrieves result value. result = result.GetType().GetProperty("Result").GetValue(result); // Checks if it is void. if (result.ToString() == "System.Threading.Tasks.VoidTaskResult") { result = null; } } // Initializes response. jResponse = new JsonResponse(); jResponse.Id = jRequest.Id; // Checks if is a wrapped result. if (result != null && result.GetType().IsGenericType&& result.GetType().GetGenericTypeDefinition() == typeof(TypedRpcReturn <>)) { // Checks if result is error. jResponse.Error = result.GetType().GetProperty("Error").GetValue(result) as JsonError; if (jResponse.Error == null) { // Retrieves result value. jResponse.Result = result.GetType().GetProperty("Data").GetValue(result); } } else { jResponse.Result = result; } return(jResponse); } catch (Exception exception) { // Declarations TypedRpcException typedRpcException; // Looks for a TypedRpcExceptions. if (TypedRpcException.FindInChain(exception, out typedRpcException)) { return(MountError(jRequest.Id, typedRpcException.Error)); } // Triggers exception event. Options.OnCatch?.Invoke(data, jRequest, context, exception); // Handles error. return(MountError(jRequest.Id, JsonError.ERROR_INTERNAL)); } }