Skip to content

Commit 6e24d5f

Browse files
authored
Merge pull request #90 from firebase/hkj-fcm-errors
Introducing FirebaseMessagingException and FCM error handling support
2 parents 48659aa + 0ce3ee5 commit 6e24d5f

19 files changed

+1115
-51
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

3-
-
3+
- [added] Added `FirebaseMessagingException` class and improved FCM error
4+
handling logic.
45

56
# v1.7.0
67

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseExceptionTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void ErrorCodeAndMessage()
3030
Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
3131
Assert.Equal("Test error message", exception.Message);
3232
Assert.Null(exception.InnerException);
33+
Assert.Null(exception.HttpResponse);
3334
}
3435

3536
[Fact]
@@ -41,6 +42,20 @@ public void InnerException()
4142
Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
4243
Assert.Equal("Test error message", exception.Message);
4344
Assert.Same(inner, exception.InnerException);
45+
Assert.Null(exception.HttpResponse);
46+
}
47+
48+
[Fact]
49+
public void HttpResponse()
50+
{
51+
var inner = new Exception("Inner exception");
52+
var resp = new HttpResponseMessage();
53+
var exception = new FirebaseException(ErrorCode.Internal, "Test error message", inner, resp);
54+
55+
Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
56+
Assert.Equal("Test error message", exception.Message);
57+
Assert.Same(inner, exception.InnerException);
58+
Assert.Same(resp, exception.HttpResponse);
4459
}
4560

4661
[Fact]
@@ -54,6 +69,7 @@ public void TimeOutError()
5469
Assert.Equal(
5570
"Timed out while making an API call: Test error message", exception.Message);
5671
Assert.Same(httpError, exception.InnerException);
72+
Assert.Null(exception.HttpResponse);
5773
}
5874

5975
[Fact]
@@ -78,6 +94,7 @@ public void NetworkUnavailableError()
7894
Assert.Equal(
7995
"Failed to establish a connection: Test error message", exception.Message);
8096
Assert.Same(httpError, exception.InnerException);
97+
Assert.Null(exception.HttpResponse);
8198
}
8299
}
83100

@@ -92,6 +109,7 @@ public void UnknownLowLevelError()
92109
"Unknown error while making a remote service call: Test error message",
93110
exception.Message);
94111
Assert.Same(httpError, exception.InnerException);
112+
Assert.Null(exception.HttpResponse);
95113
}
96114
}
97115
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Collections.Generic;
16+
using System.Net;
17+
using System.Net.Http;
18+
using System.Text;
19+
using Xunit;
20+
21+
namespace FirebaseAdmin.Tests
22+
{
23+
public class HttpErrorHandlerTest
24+
{
25+
public static readonly IEnumerable<object[]> HttpErrorCodes =
26+
new List<object[]>()
27+
{
28+
new object[] { HttpStatusCode.BadRequest, ErrorCode.InvalidArgument },
29+
new object[] { HttpStatusCode.Unauthorized, ErrorCode.Unauthenticated },
30+
new object[] { HttpStatusCode.Forbidden, ErrorCode.PermissionDenied },
31+
new object[] { HttpStatusCode.NotFound, ErrorCode.NotFound },
32+
new object[] { HttpStatusCode.Conflict, ErrorCode.Conflict },
33+
new object[] { (HttpStatusCode)429, ErrorCode.ResourceExhausted },
34+
new object[] { HttpStatusCode.InternalServerError, ErrorCode.Internal },
35+
new object[] { HttpStatusCode.ServiceUnavailable, ErrorCode.Unavailable },
36+
};
37+
38+
[Theory]
39+
[MemberData(nameof(HttpErrorCodes))]
40+
public void KnownHttpStatusCode(HttpStatusCode statusCode, ErrorCode expected)
41+
{
42+
var json = "{}";
43+
var resp = new HttpResponseMessage()
44+
{
45+
StatusCode = statusCode,
46+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
47+
};
48+
49+
var handler = new HttpErrorHandler();
50+
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
51+
52+
Assert.Equal(expected, error.ErrorCode);
53+
Assert.Equal(
54+
$"Unexpected HTTP response with status: {(int)statusCode} ({statusCode})\n{json}",
55+
error.Message);
56+
Assert.Same(resp, error.HttpResponse);
57+
Assert.Null(error.InnerException);
58+
}
59+
60+
[Theory]
61+
[MemberData(nameof(HttpErrorCodes))]
62+
public void NonJsonResponse(HttpStatusCode statusCode, ErrorCode expected)
63+
{
64+
var text = "not json";
65+
var resp = new HttpResponseMessage()
66+
{
67+
StatusCode = statusCode,
68+
Content = new StringContent(text, Encoding.UTF8, "text/plain"),
69+
};
70+
71+
var handler = new HttpErrorHandler();
72+
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, text));
73+
74+
Assert.Equal(expected, error.ErrorCode);
75+
Assert.Equal(
76+
$"Unexpected HTTP response with status: {(int)statusCode} ({statusCode})\n{text}",
77+
error.Message);
78+
Assert.Same(resp, error.HttpResponse);
79+
Assert.Null(error.InnerException);
80+
}
81+
82+
[Fact]
83+
public void UnknownHttpStatusCode()
84+
{
85+
var json = "{}";
86+
var resp = new HttpResponseMessage()
87+
{
88+
StatusCode = HttpStatusCode.MethodNotAllowed,
89+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
90+
};
91+
92+
var handler = new HttpErrorHandler();
93+
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
94+
95+
Assert.Equal(ErrorCode.Unknown, error.ErrorCode);
96+
Assert.Equal(
97+
$"Unexpected HTTP response with status: 405 (MethodNotAllowed)\n{json}",
98+
error.Message);
99+
Assert.Same(resp, error.HttpResponse);
100+
Assert.Null(error.InnerException);
101+
}
102+
}
103+
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/BatchResponseTest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public void SomeResponse()
4242
SendResponse.FromMessageId("message1"),
4343
SendResponse.FromMessageId("message2"),
4444
SendResponse.FromException(
45-
new FirebaseException(
46-
"error-message",
47-
null)),
45+
new FirebaseMessagingException(ErrorCode.Unknown, "error-message")),
4846
};
4947

5048
var batchResponse = new BatchResponse(responses);

0 commit comments

Comments
 (0)