Przeglądaj źródła

[WebApiUtils] Exception handling; BMRHandler

feature/authentication-tests
Robin Thoni 9 lat temu
rodzic
commit
328d93575e

+ 68
- 0
WebApiUtils/BusinessManager/BMRHandler.cs Wyświetl plik

@@ -0,0 +1,68 @@
1
+using System.Net;
2
+using System.Net.Http;
3
+using System.Web.Http;
4
+using iiie.Logs.DBO;
5
+
6
+namespace iiie.WebApiUtils.BusinessManager
7
+{
8
+    /// <summary>
9
+    /// Handle business manager results
10
+    /// </summary>
11
+    /// <typeparam name="T">Data type</typeparam>
12
+    public class BMRHandler<T>
13
+    {
14
+        /// <summary>
15
+        /// Converts ResultStatus codes to HttpStatusCodes codes
16
+        /// </summary>
17
+        /// <param name="status">The status code</param>
18
+        /// <returns>The http code</returns>
19
+        public static HttpStatusCode ResultStatusToHttp(ResultStatus status)
20
+        {
21
+            if (status == ResultStatus.LoginError)
22
+                return HttpStatusCode.Unauthorized;
23
+            if (status == ResultStatus.PermissionError)
24
+                return HttpStatusCode.Forbidden;
25
+            if (status == ResultStatus.InputError)
26
+                return HttpStatusCode.BadRequest;
27
+            if (status == ResultStatus.NotFound)
28
+                return HttpStatusCode.NotFound;
29
+            return HttpStatusCode.InternalServerError;
30
+        }
31
+
32
+        /// <summary>
33
+        /// Returns a string corresponding to the error
34
+        /// </summary>
35
+        /// <param name="result">The result of the operation</param>
36
+        /// <returns>The error string</returns>
37
+        public static string OpResultToString(OpResult<T> result)
38
+        {
39
+            if (result)
40
+                return result.PublicDetails;
41
+
42
+            if (result.Status == ResultStatus.LoginError)
43
+                return "Bad username/password";
44
+            if (result.Status == ResultStatus.PermissionError)
45
+                return "You don\'t have the permission to do this action";
46
+            if (result.Status == ResultStatus.InputError)
47
+                return "Invalid data was provided";
48
+            if (result.Status == ResultStatus.NotFound)
49
+                return "Ressource not found";
50
+
51
+            return "Internal error";
52
+        }
53
+
54
+        /// <summary>
55
+        /// Handle business manager results
56
+        /// </summary>
57
+        /// <param name="result">The result to handle</param>
58
+        /// <param name="request">The request to handle</param>
59
+        /// <returns>The data to return to the user</returns>
60
+        public static T Handle(OpResult<T> result, HttpRequestMessage request)
61
+        {
62
+            if (result.Status == ResultStatus.Success)
63
+                return result.Data;
64
+            var msg = request.CreateErrorResponse(ResultStatusToHttp(result.Status), OpResultToString(result));
65
+            throw new HttpResponseException(msg);
66
+        }
67
+    }
68
+}

+ 4
- 1
WebApiUtils/BusinessManager/ExceptionLoggerAttribute.cs Wyświetl plik

@@ -1,6 +1,8 @@
1 1
 using System;
2
+using System.Net.Http;
2 3
 using System.Web.Http.Filters;
3 4
 using iiie.Logs.DataAccess;
5
+using iiie.Logs.DBO;
4 6
 
5 7
 namespace iiie.WebApiUtils.BusinessManager
6 8
 {
@@ -9,7 +11,8 @@ namespace iiie.WebApiUtils.BusinessManager
9 11
     {
10 12
         public override void OnException(HttpActionExecutedContext context)
11 13
         {
12
-            Logger.Log(context.Exception);
14
+            var result = Logger.Error<int>(ResultStatus.InternalError, context.Exception);
15
+            context.Response = context.Request.CreateErrorResponse(BMRHandler<int>.ResultStatusToHttp(result.Status), BMRHandler<int>.OpResultToString(result));
13 16
         }
14 17
     }
15 18
 }

+ 1
- 0
WebApiUtils/WebApiUtils.csproj Wyświetl plik

@@ -76,6 +76,7 @@
76 76
     <Reference Include="System.Xml" />
77 77
   </ItemGroup>
78 78
   <ItemGroup>
79
+    <Compile Include="BusinessManager\BMRHandler.cs" />
79 80
     <Compile Include="BusinessManager\CheckModelForNullAttribute.cs" />
80 81
     <Compile Include="BusinessManager\ExceptionLoggerAttribute.cs" />
81 82
     <Compile Include="BusinessManager\ValidateModelStateAttribute.cs" />

Ładowanie…
Anuluj
Zapisz