public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception exception)
            {
                var identityServerException = exception as IdentityServerException;
                if (identityServerException == null)
                {
                    identityServerException = new IdentityServerException(UnhandledExceptionCode, exception.Message);
                }

                var code    = identityServerException.Code;
                var message = identityServerException.Message;
                // context.Response.Clear();
                var error = new ErrorResponse();
                PopulateError(error, identityServerException);
                context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                context.Response.ContentType = "application/json";
                var serializedError = error.SerializeWithDataContract();
                await context.Response.WriteAsync(serializedError);
            }
        }
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception exception)
            {
                var simpleIdentityServerEventSource  = _options.SimpleIdentityServerEventSource;
                var identityServerException          = exception as IdentityServerException;
                var identityServerExceptionWithState = exception as IdentityServerExceptionWithState;
                if (identityServerException == null)
                {
                    identityServerException = new IdentityServerException(ErrorCodes.UnhandledExceptionCode, exception.Message);
                    simpleIdentityServerEventSource.Failure(exception);
                }
                else
                {
                    var code    = identityServerException.Code;
                    var message = identityServerException.Message;
                    var state   = identityServerExceptionWithState == null
                        ? string.Empty
                        : identityServerExceptionWithState.State;
                    simpleIdentityServerEventSource.OpenIdFailure(code, message, state);
                }

                context.Response.Clear();
                if (identityServerExceptionWithState != null)
                {
                    var errorResponseWithState = new ErrorResponseWithState
                    {
                        State = identityServerExceptionWithState.State
                    };

                    PopulateError(errorResponseWithState, identityServerExceptionWithState);
                    context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                    context.Response.ContentType = "application/json";
                    var serializedError = errorResponseWithState.SerializeWithDataContract();
                    await context.Response.WriteAsync(serializedError);
                }
                else
                {
                    var error = new ErrorResponse();
                    PopulateError(error, identityServerException);
                    context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                    context.Response.ContentType = "application/json";
                    var serializedError = error.SerializeWithDataContract();
                    await context.Response.WriteAsync(serializedError);
                }
            }
        }
 public IdentityServerExceptionWithState(IdentityServerException ex, string state) : base(ex.Code, ex.Message, ex)
 {
     State = state;
 }
 private static void PopulateError(ErrorResponse errorResponse, IdentityServerException exception)
 {
     errorResponse.Error            = exception.Code;
     errorResponse.ErrorDescription = exception.Message;
 }