forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonApiException.cs
More file actions
88 lines (82 loc) · 2.73 KB
/
Copy pathJsonApiException.cs
File metadata and controls
88 lines (82 loc) · 2.73 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
using System;
using System.Net;
namespace JSONAPI.Documents.Builders
{
/// <summary>
/// Exception that should be thrown by document builders if an error occurs. The data in
/// this exception will drive the construction of the error object in the response document,
/// as well as the HTTP status code.
/// </summary>
public class JsonApiException : Exception
{
/// <summary>
/// The error
/// </summary>
public IError Error { get; set; }
/// <summary>
/// Creates a new JsonApiException
/// </summary>
/// <param name="error"></param>
public JsonApiException(IError error)
{
Error = error;
}
/// <summary>
/// Creates a JsonApiException indicating a problem with a supplied query parameter
/// </summary>
public static JsonApiException CreateForParameterError(string title, string detail, string parameter)
{
var error = new Error
{
Id = Guid.NewGuid().ToString(),
Status = HttpStatusCode.BadRequest,
Title = title,
Detail = detail,
Parameter = parameter
};
return new JsonApiException(error);
}
/// <summary>
/// Creates a JsonApiException with a title and detail
/// </summary>
public static JsonApiException Create(string title, string detail, HttpStatusCode status)
{
var error = new Error
{
Id = Guid.NewGuid().ToString(),
Status = status,
Title = title,
Detail = detail
};
return new JsonApiException(error);
}
/// <summary>
/// Creates a JsonApiException to send a 404 Not Found error.
/// </summary>
public static JsonApiException CreateForNotFound(string detail = null)
{
var error = new Error
{
Id = Guid.NewGuid().ToString(),
Status = HttpStatusCode.NotFound,
Title = "Resource not found",
Detail = detail
};
return new JsonApiException(error);
}
/// <summary>
/// Creates a JsonApiException to send a 403 Forbidden error.
/// </summary>
public static JsonApiException CreateForForbidden(string detail = null)
{
var error = new Error
{
Id = Guid.NewGuid().ToString(),
Status = HttpStatusCode.Forbidden,
Title = "Forbidden",
Detail = detail
};
return new JsonApiException(error);
}
}
}