-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathErrorFormatter.cs
More file actions
105 lines (91 loc) · 3.13 KB
/
ErrorFormatter.cs
File metadata and controls
105 lines (91 loc) · 3.13 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
using System;
using System.Net;
using System.Threading.Tasks;
using JSONAPI.Documents;
using Newtonsoft.Json;
namespace JSONAPI.Json
{
/// <summary>
/// Default implementation of IErrorFormatter
/// </summary>
public class ErrorFormatter : IErrorFormatter
{
private readonly ILinkFormatter _linkFormatter;
private readonly IMetadataFormatter _metadataFormatter;
/// <summary>
/// Creates a new errorFormatter
/// </summary>
/// <param name="linkFormatter"></param>
/// <param name="metadataFormatter"></param>
public ErrorFormatter(ILinkFormatter linkFormatter, IMetadataFormatter metadataFormatter)
{
_linkFormatter = linkFormatter;
_metadataFormatter = metadataFormatter;
}
public Task Serialize(IError error, JsonWriter writer)
{
writer.WriteStartObject();
if (error.Id != null)
{
writer.WritePropertyName("id");
writer.WriteValue(error.Id);
}
if (error.AboutLink != null)
{
writer.WritePropertyName("links");
writer.WriteStartObject();
writer.WritePropertyName("about");
_linkFormatter.Serialize(error.AboutLink, writer);
writer.WriteEndObject();
}
if (error.Status != default(HttpStatusCode))
{
writer.WritePropertyName("status");
writer.WriteValue(((int)error.Status).ToString());
}
if (error.Code != null)
{
writer.WritePropertyName("code");
writer.WriteValue(error.Code);
}
if (error.Title != null)
{
writer.WritePropertyName("title");
writer.WriteValue(error.Title);
}
if (error.Detail != null)
{
writer.WritePropertyName("detail");
writer.WriteValue(error.Detail);
}
if (error.Pointer != null || error.Parameter != null)
{
writer.WritePropertyName("source");
writer.WriteStartObject();
if (error.Pointer != null)
{
writer.WritePropertyName("pointer");
writer.WriteValue(error.Pointer);
}
if (error.Parameter != null)
{
writer.WritePropertyName("parameter");
writer.WriteValue(error.Parameter);
}
writer.WriteEndObject();
}
if (error.Metadata != null)
{
writer.WritePropertyName("meta");
_metadataFormatter.Serialize(error.Metadata, writer);
}
writer.WriteEndObject();
return Task.FromResult(0);
}
public Task<IError> Deserialize(JsonReader reader, string currentPath)
{
// The client should never be sending us errors
throw new NotSupportedException();
}
}
}