forked from TypedRest/OpenServiceBroker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrokerException.cs
More file actions
73 lines (66 loc) · 2.8 KB
/
BrokerException.cs
File metadata and controls
73 lines (66 loc) · 2.8 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
using System;
using System.Net;
namespace OpenServiceBroker.Errors
{
/// <summary>
/// The request to the Service Broker failed.
/// </summary>
public class BrokerException : Exception
{
public string ErrorCode { get; }
public HttpStatusCode HttpCode { get; }
public BrokerException(string message, string errorCode, HttpStatusCode httpCode = (HttpStatusCode)422 /*UnprocessableEntity*/)
: base(message)
{
ErrorCode = errorCode;
HttpCode = httpCode;
}
/// <summary>
/// Serializes the exception to an error response object.
/// </summary>
public Error ToResponse() => new Error
{
ErrorCode = ErrorCode,
Description = Message
};
/// <summary>
/// Deserializes the exception from an error response object.
/// </summary>
public static BrokerException FromResponse(Error dto, HttpStatusCode statusCode)
{
switch (dto.ErrorCode)
{
case ApiVersionNotSupportedException.ErrorCode:
return new ApiVersionNotSupportedException(dto.Description);
case AsyncRequiredException.ErrorCode:
return new AsyncRequiredException(dto.Description);
case BadRequestException.ErrorCode:
return new BadRequestException(dto.Description);
case ConcurrencyException.ErrorCode:
return new ConcurrencyException(dto.Description);
case ConflictException.ErrorCode:
return new ConflictException(dto.Description);
case GoneException.ErrorCode:
return new GoneException(dto.Description);
case NotFoundException.ErrorCode:
return new NotFoundException(dto.Description);
case RequiresAppException.ErrorCode:
return new RequiresAppException(dto.Description);
}
switch (statusCode)
{
case HttpStatusCode.PreconditionFailed:
return new ApiVersionNotSupportedException(dto.Description);
case HttpStatusCode.BadRequest:
return new BadRequestException(dto.Description);
case HttpStatusCode.Conflict:
return new ConflictException(dto.Description);
case HttpStatusCode.Gone:
return new GoneException(dto.Description);
case HttpStatusCode.NotFound:
return new NotFoundException(dto.Description);
}
return new BrokerException(dto.ErrorCode, dto.Description);
}
}
}