-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathWorkloadExceptionBase.cs
More file actions
98 lines (78 loc) · 3.16 KB
/
Copy pathWorkloadExceptionBase.cs
File metadata and controls
98 lines (78 loc) · 3.16 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// <copyright company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
using Fabric_Extension_BE_Boilerplate.Constants;
using Fabric_Extension_BE_Boilerplate.Contracts.FabricAPI.Workload;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text.Json;
namespace Fabric_Extension_BE_Boilerplate.Exceptions
{
public abstract class WorkloadExceptionBase : Exception
{
private static readonly IList<string> EmptyStringList = new string[0];
private string _messageTemplate;
private IList<string> _messageParameters;
protected WorkloadExceptionBase(
int httpStatusCode,
string errorCode,
string messageTemplate,
IList<string> messageParameters,
ErrorSource errorSource,
bool isPermanent)
{
HttpStatusCode = httpStatusCode;
ErrorCode = errorCode;
ErrorSource = errorSource;
IsPermanent = isPermanent;
_messageTemplate = messageTemplate;
_messageParameters = messageParameters ?? EmptyStringList;
}
public virtual int HttpStatusCode { get; }
public virtual string ErrorCode { get; }
public virtual string ErrorMessage => string.Format(_messageTemplate, _messageParameters.ToArray());
public virtual ErrorSource ErrorSource { get; }
public virtual bool IsPermanent { get; }
public override string Message => ErrorMessage;
public IList<ErrorExtendedInformation> Details { get; private set; }
public virtual ContentResult ToHttpActionResult()
{
var errorResponse = new ErrorResponse
{
ErrorCode = ErrorCode,
Message = ErrorMessage,
MessageParameters = _messageParameters.Any() ? _messageParameters : null,
Source = ErrorSource,
IsPermanent = IsPermanent,
MoreDetails = Details,
};
return new ContentResult
{
StatusCode = (int)HttpStatusCode,
Content = JsonSerializer.Serialize(errorResponse),
ContentType = MediaTypeNames.Application.Json,
};
}
public virtual string ToTelemetryString() => ErrorMessage;
public WorkloadExceptionBase WithDetail(string errorCode, string messageTemplate, params (string name, string value)[] parameters )
{
var parameterValues = parameters.Select(p => p.value).ToArray();
var detail = new ErrorExtendedInformation
{
ErrorCode = errorCode,
Message = string.Format(messageTemplate, parameterValues),
MessageParameters = parameterValues,
AdditionalParameters = parameters.Select(p => new NameValuePair { Name = p.name, Value = p.value }).ToList(),
};
if (Details == null)
{
Details = new List<ErrorExtendedInformation>();
}
Details.Add(detail);
return this;
}
}
}