aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Middleware/ExceptionMiddleware.cs
blob: 39aace95d2008a55f74fb86eb080a00abb000358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Jellyfin.Api.Models.ExceptionDtos;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Server.Middleware
{
    /// <summary>
    /// Exception Middleware.
    /// </summary>
    public class ExceptionMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger<ExceptionMiddleware> _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExceptionMiddleware"/> class.
        /// </summary>
        /// <param name="next">Next request delegate.</param>
        /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
        public ExceptionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
        {
            _next = next ?? throw new ArgumentNullException(nameof(next));
            _logger = loggerFactory.CreateLogger<ExceptionMiddleware>() ??
                      throw new ArgumentNullException(nameof(loggerFactory));
        }

        /// <summary>
        /// Invoke request.
        /// </summary>
        /// <param name="context">Request context.</param>
        /// <returns>Task.</returns>
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                if (context.Response.HasStarted)
                {
                    _logger.LogWarning("The response has already started, the exception middleware will not be executed.");
                    throw;
                }

                var exceptionBody = new ExceptionDto { Message = ex.Message };
                var exceptionJson = JsonSerializer.Serialize(exceptionBody);

                context.Response.Clear();
                context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                // TODO switch between PascalCase and camelCase
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(exceptionJson).ConfigureAwait(false);
            }
        }
    }
}