forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpError.cs
More file actions
147 lines (116 loc) · 4.57 KB
/
HttpError.cs
File metadata and controls
147 lines (116 loc) · 4.57 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Net;
using ServiceStack.Model;
using ServiceStack.Web;
namespace ServiceStack
{
public class HttpError : Exception, IHttpError, IResponseStatusConvertible
{
public HttpError() : this(null) { }
public HttpError(string message)
: this(HttpStatusCode.InternalServerError, message)
{ }
public HttpError(HttpStatusCode statusCode)
: this(statusCode, statusCode.ToString(), null)
{ }
public HttpError(HttpStatusCode statusCode, string errorMessage)
: this(statusCode, statusCode.ToString(), errorMessage)
{ }
public HttpError(int statusCode, string errorCode)
: this(statusCode, errorCode, null)
{ }
public HttpError(object responseDto, HttpStatusCode statusCode, string errorCode, string errorMessage)
: this(statusCode, errorCode, errorMessage)
{
this.Response = responseDto;
}
public HttpError(object responseDto, int statusCode, string errorCode, string errorMessage = null, Exception innerException = null)
: this(statusCode, errorCode, errorMessage, innerException)
{
this.Response = responseDto;
}
public HttpError(HttpStatusCode statusCode, string errorCode, string errorMessage)
: this((int)statusCode, errorCode, errorMessage)
{ }
public HttpError(int statusCode, string errorCode, string errorMessage, Exception innerException = null)
: base(errorMessage ?? errorCode ?? statusCode.ToString(), innerException)
{
this.ErrorCode = errorCode ?? statusCode.ToString();
this.Status = statusCode;
this.Headers = new Dictionary<string, string>();
this.StatusDescription = errorCode;
this.Headers = new Dictionary<string, string>();
this.Cookies = new List<Cookie>();
}
public HttpError(HttpStatusCode statusCode, Exception innerException)
: this(innerException.Message, innerException)
{
this.StatusCode = statusCode;
}
public HttpError(string message, Exception innerException) : base(message, innerException)
{
if (innerException != null)
{
this.ErrorCode = innerException.GetType().Name;
}
this.Headers = new Dictionary<string, string>();
this.Cookies = new List<Cookie>();
}
public string ErrorCode { get; set; }
public string ContentType { get; set; }
public Dictionary<string, string> Headers { get; private set; }
public List<Cookie> Cookies { get; private set; }
public int Status { get; set; }
public HttpStatusCode StatusCode
{
get { return (HttpStatusCode)Status; }
set { Status = (int)value; }
}
public string StatusDescription { get; set; }
public object Response { get; set; }
public IContentTypeWriter ResponseFilter { get; set; }
public IRequest RequestContext { get; set; }
public int PaddingLength { get; set; }
public Func<IDisposable> ResultScope { get; set; }
public IDictionary<string, string> Options
{
get { return this.Headers; }
}
public ResponseStatus ResponseStatus
{
get
{
return this.Response.GetResponseStatus();
}
}
public List<ResponseError> GetFieldErrors()
{
var responseStatus = ResponseStatus;
if (responseStatus != null)
return responseStatus.Errors ?? new List<ResponseError>();
return new List<ResponseError>();
}
public static Exception NotFound(string message)
{
return new HttpError(HttpStatusCode.NotFound, message);
}
public static Exception Unauthorized(string message)
{
return new HttpError(HttpStatusCode.Unauthorized, message);
}
public static Exception Conflict(string message)
{
return new HttpError(HttpStatusCode.Conflict, message);
}
public static Exception Forbidden(string message)
{
return new HttpError(HttpStatusCode.Forbidden, message);
}
public ResponseStatus ToResponseStatus()
{
return Response.GetResponseStatus()
?? ResponseStatusUtils.CreateResponseStatus(ErrorCode, Message, null);
}
}
}