LCOV - code coverage report
Current view: top level - Cqrs.WebApi/Formatters/FormMultipart/Infrastructure/Logger - FormDataConverterLogger.cs Hit Total Coverage
Test: doc-coverage.info Lines: 3 10 30.0 %
Date: 2017-07-26

          Line data    Source code
       1             : using System;
       2             : using System.Collections.Generic;
       3             : using System.Linq;
       4             : 
       5             : namespace Cqrs.WebApi.Formatters.FormMultipart.Infrastructure.Logger
       6             : {
       7             :         public class FormDataConverterLogger : IFormDataConverterLogger
       8           0 :         {
       9             :                 private Dictionary<string, List<LogErrorInfo>> Errors { get; set; }
      10             : 
      11           0 :                 public FormDataConverterLogger()
      12             :                 {
      13             :                         Errors = new Dictionary<string, List<LogErrorInfo>>();
      14             :                 }
      15             : 
      16           1 :                 public void LogError(string errorPath, Exception exception)
      17             :                 {
      18             :                         AddError(errorPath, new LogErrorInfo(exception));
      19             :                 }
      20             : 
      21           1 :                 public void LogError(string errorPath, string errorMessage)
      22             :                 {
      23             :                         AddError(errorPath, new LogErrorInfo(errorMessage));
      24             :                 }
      25             : 
      26           0 :                 public List<LogItem> GetErrors()
      27             :                 {
      28             :                         return Errors.Select(m => new LogItem()
      29             :                         {
      30             :                                 ErrorPath = m.Key,
      31             :                                 Errors = m.Value.Select(t => t).ToList()
      32             :                         }).ToList();
      33             :                 }
      34             : 
      35           1 :                 public void EnsureNoErrors()
      36             :                 {
      37             :                         if (Errors.Any())
      38             :                         {
      39             :                                 var errors = Errors
      40             :                                         .Select(m => String.Format("{0}: {1}", m.Key, String.Join(". ", m.Value.Select(x => (x.ErrorMessage ?? (x.Exception != null ? x.Exception.Message : ""))))))
      41             :                                         .ToList();
      42             : 
      43             :                                 string errorMessage = String.Join(" ", errors);
      44             : 
      45             :                                 throw new Exception(errorMessage);
      46             :                         }
      47             :                 }
      48             : 
      49             :                 private void AddError(string errorPath, LogErrorInfo info)
      50             :                 {
      51             :                         List<LogErrorInfo> listErrors;
      52             :                         if (!Errors.TryGetValue(errorPath, out listErrors))
      53             :                         {
      54             :                                 listErrors = new List<LogErrorInfo>();
      55             :                                 Errors.Add(errorPath, listErrors);
      56             :                         }
      57             :                         listErrors.Add(info);
      58             :                 }
      59             : 
      60             :                 public class LogItem
      61           0 :                 {
      62             :                         public string ErrorPath { get; set; }
      63             :                         public List<LogErrorInfo> Errors { get; set; }
      64             :                 }
      65             : 
      66             :                 public class LogErrorInfo
      67           0 :                 {
      68             :                         public string ErrorMessage { get; private set; }
      69             :                         public Exception Exception { get; private set; }
      70             :                         public bool IsException { get; private set; }
      71             : 
      72           0 :                         public LogErrorInfo(string errorMessage)
      73             :                         {
      74             :                                 ErrorMessage = errorMessage;
      75             :                                 IsException = false;
      76             :                         }
      77             : 
      78           0 :                         public LogErrorInfo(Exception exception)
      79             :                         {
      80             :                                 Exception = exception;
      81             :                                 IsException = true;
      82             :                         }
      83             :                 }
      84             :         }
      85             : }

Generated by: LCOV version 1.10