Skip to content

Commit 4a5b618

Browse files
authored
Merge pull request #92 from firebase/hkj-http-client
Implemented ErrorHandlingHttpClient for easy and typesafe error handling
2 parents 78714da + 41fef44 commit 4a5b618

19 files changed

+807
-172
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/HttpErrorHandlerTest.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class HttpErrorHandlerTest
3535
new object[] { HttpStatusCode.ServiceUnavailable, ErrorCode.Unavailable },
3636
};
3737

38+
private static readonly TestHttpErrorHandler ErrorHandler = new TestHttpErrorHandler();
39+
3840
[Theory]
3941
[MemberData(nameof(HttpErrorCodes))]
4042
public void KnownHttpStatusCode(HttpStatusCode statusCode, ErrorCode expected)
@@ -46,8 +48,7 @@ public void KnownHttpStatusCode(HttpStatusCode statusCode, ErrorCode expected)
4648
Content = new StringContent(json, Encoding.UTF8, "application/json"),
4749
};
4850

49-
var handler = new HttpErrorHandler();
50-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
51+
var error = ErrorHandler.HandleHttpErrorResponse(resp, json);
5152

5253
Assert.Equal(expected, error.ErrorCode);
5354
Assert.Equal(
@@ -68,8 +69,7 @@ public void NonJsonResponse(HttpStatusCode statusCode, ErrorCode expected)
6869
Content = new StringContent(text, Encoding.UTF8, "text/plain"),
6970
};
7071

71-
var handler = new HttpErrorHandler();
72-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, text));
72+
var error = ErrorHandler.HandleHttpErrorResponse(resp, text);
7373

7474
Assert.Equal(expected, error.ErrorCode);
7575
Assert.Equal(
@@ -89,8 +89,7 @@ public void UnknownHttpStatusCode()
8989
Content = new StringContent(json, Encoding.UTF8, "application/json"),
9090
};
9191

92-
var handler = new HttpErrorHandler();
93-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
92+
var error = ErrorHandler.HandleHttpErrorResponse(resp, json);
9493

9594
Assert.Equal(ErrorCode.Unknown, error.ErrorCode);
9695
Assert.Equal(
@@ -99,5 +98,13 @@ public void UnknownHttpStatusCode()
9998
Assert.Same(resp, error.HttpResponse);
10099
Assert.Null(error.InnerException);
101100
}
102-
}
101+
102+
private class TestHttpErrorHandler : HttpErrorHandler<FirebaseException>
103+
{
104+
protected override FirebaseException CreateException(FirebaseExceptionArgs args)
105+
{
106+
return new FirebaseException(args.Code, args.Message, response: args.HttpResponse);
107+
}
108+
}
109+
}
103110
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessagingErrorHandlerTest.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public void PlatformError()
5050
Content = new StringContent(json, Encoding.UTF8, "application/json"),
5151
};
5252

53-
var handler = new MessagingErrorHandler();
54-
var error = Assert.Throws<FirebaseMessagingException>(() => handler.ThrowIfError(resp, json));
53+
var error = MessagingErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
5554

5655
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
5756
Assert.Equal("Test error message", error.Message);
@@ -82,8 +81,7 @@ public void KnownMessagingErrorCode(string code, MessagingErrorCode expected)
8281
Content = new StringContent(json, Encoding.UTF8, "application/json"),
8382
};
8483

85-
var handler = new MessagingErrorHandler();
86-
var error = Assert.Throws<FirebaseMessagingException>(() => handler.ThrowIfError(resp, json));
84+
var error = MessagingErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
8785

8886
Assert.Equal(ErrorCode.PermissionDenied, error.ErrorCode);
8987
Assert.Equal("Test error message", error.Message);
@@ -113,8 +111,7 @@ public void UnknownMessagingErrorCode()
113111
Content = new StringContent(json, Encoding.UTF8, "application/json"),
114112
};
115113

116-
var handler = new MessagingErrorHandler();
117-
var error = Assert.Throws<FirebaseMessagingException>(() => handler.ThrowIfError(resp, json));
114+
var error = MessagingErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
118115

119116
Assert.Equal(ErrorCode.PermissionDenied, error.ErrorCode);
120117
Assert.Equal("Test error message", error.Message);
@@ -133,8 +130,7 @@ public void NoDetails()
133130
Content = new StringContent(json, Encoding.UTF8, "application/json"),
134131
};
135132

136-
var handler = new MessagingErrorHandler();
137-
var error = Assert.Throws<FirebaseMessagingException>(() => handler.ThrowIfError(resp, json));
133+
var error = MessagingErrorHandler.Instance.HandleHttpErrorResponse(resp, json);
138134

139135
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
140136
Assert.Equal(

FirebaseAdmin/FirebaseAdmin.Tests/PlatformErrorHandlerTest.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace FirebaseAdmin.Tests
2121
{
2222
public class PlatformErrorHandlerTest
2323
{
24+
private static readonly TestPlatformErrorHandler ErrorHandler = new TestPlatformErrorHandler();
25+
2426
[Fact]
2527
public void PlatformError()
2628
{
@@ -36,8 +38,7 @@ public void PlatformError()
3638
Content = new StringContent(json, Encoding.UTF8, "application/json"),
3739
};
3840

39-
var handler = new PlatformErrorHandler();
40-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
41+
var error = ErrorHandler.HandleHttpErrorResponse(resp, json);
4142

4243
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
4344
Assert.Equal("Test error message", error.Message);
@@ -55,8 +56,7 @@ public void NonJsonResponse()
5556
Content = new StringContent(text, Encoding.UTF8, "text/plain"),
5657
};
5758

58-
var handler = new PlatformErrorHandler();
59-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, text));
59+
var error = ErrorHandler.HandleHttpErrorResponse(resp, text);
6060

6161
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
6262
Assert.Equal(
@@ -80,8 +80,7 @@ public void PlatformErrorWithoutCode()
8080
Content = new StringContent(json, Encoding.UTF8, "application/json"),
8181
};
8282

83-
var handler = new PlatformErrorHandler();
84-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
83+
var error = ErrorHandler.HandleHttpErrorResponse(resp, json);
8584

8685
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
8786
Assert.Equal("Test error message", error.Message);
@@ -103,8 +102,7 @@ public void PlatformErrorWithoutMessage()
103102
Content = new StringContent(json, Encoding.UTF8, "application/json"),
104103
};
105104

106-
var handler = new PlatformErrorHandler();
107-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
105+
var error = ErrorHandler.HandleHttpErrorResponse(resp, json);
108106

109107
Assert.Equal(ErrorCode.InvalidArgument, error.ErrorCode);
110108
Assert.Equal(
@@ -124,8 +122,7 @@ public void PlatformErrorWithoutCodeOrMessage()
124122
Content = new StringContent(json, Encoding.UTF8, "application/json"),
125123
};
126124

127-
var handler = new PlatformErrorHandler();
128-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
125+
var error = ErrorHandler.HandleHttpErrorResponse(resp, json);
129126

130127
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
131128
Assert.Equal(
@@ -134,5 +131,13 @@ public void PlatformErrorWithoutCodeOrMessage()
134131
Assert.Same(resp, error.HttpResponse);
135132
Assert.Null(error.InnerException);
136133
}
134+
135+
private class TestPlatformErrorHandler : PlatformErrorHandler<FirebaseException>
136+
{
137+
protected override FirebaseException CreateException(FirebaseExceptionArgs args)
138+
{
139+
return new FirebaseException(args.Code, args.Message, response: args.HttpResponse);
140+
}
141+
}
137142
}
138143
}

0 commit comments

Comments
 (0)