public static IJsonRpcHandlerBuilder UseSecurityExceptionHandler(this IJsonRpcHandlerBuilder builder) { return(builder.Use(async(context, next) => { try { await next(); } catch (Exception exception) { var securityException = exception.Unwrap <SecurityException>(); if (securityException != null) { // This is wrong: // 1) we are using exceptions for flow control // 2) sematically `SecurityException` means more `Forbidden` than `Unauthorized` // However - this get's the job done and I can't think of any better alternative without // introducing ton of boilerplate code. context.SetResponse(JsonRpcError.UNAUTHORIZED, $"{JsonRpcError.GetMessage(JsonRpcError.UNAUTHORIZED)} - {securityException.Message}"); } else { throw; } } })); }
public JsonRpcException(int code, object data = null) : base(JsonRpcError.GetMessage(code)) { JsonRpcCode = code; JsonRpcData = data; }
public JsonRpcResponse(object id, int errorCode, object errorData = null) : this(id, errorCode, JsonRpcError.GetMessage(errorCode), errorData : errorData) { }