-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCatalystBaseException.cs
54 lines (40 loc) · 1.73 KB
/
CatalystBaseException.cs
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
using OrderCloud.SDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace OrderCloud.Catalyst
{
public class CatalystBaseException : Exception
{
public override string Message => Errors?.FirstOrDefault()?.Message ?? "";
public HttpStatusCode HttpStatus { get; set; }
public IList<ApiError> Errors { get; }
public CatalystBaseException(ApiError apiError, HttpStatusCode httpStatus = HttpStatusCode.BadRequest)
: this(apiError.ErrorCode, apiError.Message, apiError.Data, httpStatus) { }
public CatalystBaseException(IList<ApiError> errors, HttpStatusCode httpStatus = HttpStatusCode.BadRequest)
{
HttpStatus = httpStatus;
Require.That(!errors.IsNullOrEmpty(), new Exception("errors collection must contain at least one item."));
Errors = errors;
}
public CatalystBaseException(string errorCode, string message, object data = null, HttpStatusCode httpStatus = HttpStatusCode.BadRequest)
{
HttpStatus = httpStatus;
Errors = new[] {
new ApiError {
ErrorCode = errorCode,
Message = message,
Data = data
}
};
}
public CatalystBaseException(ErrorCode errorCode, object data = null)
: this(errorCode.Code, errorCode.DefaultMessage, data, errorCode.HttpStatus) { }
// Keeping these depreacated constructors that take an Int for backwards compatibility.
public CatalystBaseException(ApiError apiError, int httpStatus) : this(apiError, (HttpStatusCode)httpStatus) { }
public CatalystBaseException(IList<ApiError> errors, int httpStatus) : this(errors, (HttpStatusCode)httpStatus) { }
public CatalystBaseException(string errorCode, string message, object data, int httpStatus)
: this(errorCode, message, data, (HttpStatusCode)httpStatus) { }
}
}