Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //if (env.IsDevelopment())
            //{
            //    // Don't use this in production.  It gives a very pretty stack trace and shows in the code where the exception occurred.
            //    app.UseDeveloperExceptionPage();
            //}
            //else
            //{
            //    // This redirects the flow of the request to a different path entirely, that path returns an appropriate content which is then given a 500 status code
            //    app.UseExceptionHandler("/home/error");

            //    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            //    // HSTS = "HTTP Strict Transport Security Protocol"
            //    //app.UseHsts();
            //}

            app
            // This approach does not require a separate controller to handle error responses and is more suited to APIs
            .UseExceptionHandler(errorApp => errorApp.Run(async context => await UnexpectedExceptionHandler.Invoke(context)))

            // Ensure we use Basic Authentication
            .UseAuthentication()

            .UseHttpsRedirection()

            // Plug in the MVC framework
            .UseMvc()

            // Add Swagger
            .UseSwagger()
            .UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", "SLCS v2"));
        }
Ejemplo n.º 2
0
        public async Task UnhandledExceptionsReturnA500()
        {
            var context = new DefaultHttpContext();

            context.AddExceptionToContext(new ApplicationException("an exception"));

            await UnexpectedExceptionHandler.Invoke(context);

            Assert.AreEqual(500, context.Response.StatusCode);
        }
Ejemplo n.º 3
0
        public async Task UnhandledValidationExceptionReturnA422()
        {
            var context = new DefaultHttpContext();

            var validationException = new SlcsValidationException(ValidationExceptionSeverity.Error, "a message",
                                                                  SlcsErrors.WrapError(new SlcsError {
                code = "123"
            }));

            context.AddExceptionToContext(validationException);

            await UnexpectedExceptionHandler.Invoke(context);

            Assert.AreEqual(422, context.Response.StatusCode);
        }
 private void Awake()
 {
     AppDomain.CurrentDomain.UnhandledException += OnUnexpectedException;
     Application.logMessageReceived             += OnUnityLogReceived;
     if (File.Exists("exceptions.log"))
     {
         File.Delete("exceptions.log");
     }
     if (Instance == null)
     {
         Instance = this;
     }
     else
     {
         Destroy(this);
         Debug.LogError("Error: Unexpected multiple instances of UnexpectedExceptionHandler");
     }
 }
Ejemplo n.º 5
0
        public async Task UnhandledException_BodySerialisesToA_SlcsErrors_Instance()
        {
            var context = new DefaultHttpContext();

            // We need to initialise the fake httpContext to get to the body
            context.Response.Body = new MemoryStream();

            var ex = new ApplicationException("Some exception occurred");

            context.AddExceptionToContext(ex);

            await UnexpectedExceptionHandler.Invoke(context);

            string body = await context.GetBodyFromResponse();

            var expectedErrorObject = JsonConvert.DeserializeObject <SlcsErrors>(body);

            Assert.IsNotNull(expectedErrorObject);
        }
Ejemplo n.º 6
0
        public async Task UnhandledValidationException_BodySerialisesToA_SlcsErrors_Instance()
        {
            var context = new DefaultHttpContext();

            // We need to initialise the fake httpContext to get to the body
            context.Response.Body = new MemoryStream();

            var ex = new SlcsValidationException(ValidationExceptionSeverity.Error, "a message",
                                                 SlcsErrors.WrapError(new SlcsError {
                code = "123"
            }));

            context.AddExceptionToContext(ex);

            await UnexpectedExceptionHandler.Invoke(context);

            string body = await context.GetBodyFromResponse();

            var expectedErrorObject = JsonConvert.DeserializeObject <SlcsErrors>(body);

            Assert.IsNotNull(expectedErrorObject);
        }