|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 5 | + |
| 6 | +namespace PowerUtils.AspNetCore.ErrorHandler |
| 7 | +{ |
| 8 | + internal static class ModelStateExtensions |
| 9 | + { |
| 10 | + private const string BODY_PROPERTY_NAME = "RequestBody"; |
| 11 | + |
| 12 | + public static IDictionary<string, string> MappingModelState(this ModelStateDictionary modelState) |
| 13 | + { |
| 14 | + var modelStateErrors = modelState.Where(s => s.Value.Errors.Count > 0); |
| 15 | + |
| 16 | + var errors = new Dictionary<string, string>(); |
| 17 | + foreach(var modelStateError in modelStateErrors) |
| 18 | + { |
| 19 | + (var property, var error) = modelStateError._mappingModelStateErrors(); |
| 20 | + errors.Add(property, error); |
| 21 | + } |
| 22 | + |
| 23 | + return errors; |
| 24 | + } |
| 25 | + |
| 26 | + public static IDictionary<string, string> CheckPayloadTooLargeAndReturnError(this ModelStateDictionary modelState) |
| 27 | + { |
| 28 | + var modelStateErrors = modelState.Where(s => s.Value.Errors.Count > 0); |
| 29 | + if(modelStateErrors.Count() != 1) |
| 30 | + { |
| 31 | + return new Dictionary<string, string>(); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + var errorMessage = modelStateErrors |
| 36 | + .Single() |
| 37 | + .Value.Errors |
| 38 | + .Select(s => s.ErrorMessage) |
| 39 | + .First() |
| 40 | + .ToLower(); |
| 41 | + |
| 42 | + if("failed to read the request form. multipart body length limit 1048576 exceeded.".Equals(errorMessage)) |
| 43 | + { |
| 44 | + var maxSize = errorMessage |
| 45 | + .Replace("failed to read the request form. multipart body length limit ", "") |
| 46 | + .Replace(" exceeded.", ""); |
| 47 | + |
| 48 | + return new Dictionary<string, string>() |
| 49 | + { |
| 50 | + { BODY_PROPERTY_NAME, "MAX:" + maxSize } |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + return new Dictionary<string, string>(); |
| 55 | + } |
| 56 | + |
| 57 | + private static (string Property, string Error) _mappingModelStateErrors(this KeyValuePair<string, ModelStateEntry> modelStateError) |
| 58 | + { |
| 59 | + if(modelStateError.Key.StartsWith("$.")) |
| 60 | + { |
| 61 | + return ( |
| 62 | + modelStateError.Key[2..], |
| 63 | + "INVALID" |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + var error = modelStateError |
| 68 | + .Value |
| 69 | + .Errors |
| 70 | + .Select(s => s.ErrorMessage) |
| 71 | + .First(); |
| 72 | + |
| 73 | + return _mappingError(modelStateError.Key, error); |
| 74 | + } |
| 75 | + |
| 76 | + private static (string Property, string Error) _mappingError(string property, string error) |
| 77 | + { |
| 78 | + if(property == "$") |
| 79 | + { |
| 80 | + return (BODY_PROPERTY_NAME, "INVALID"); |
| 81 | + } |
| 82 | + |
| 83 | + if("A non-empty request body is required.".Equals(error, StringComparison.InvariantCultureIgnoreCase)) |
| 84 | + { |
| 85 | + return (BODY_PROPERTY_NAME, "REQUIRED"); |
| 86 | + } |
| 87 | + |
| 88 | + return (property, error); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments