Skip to content

Commit 41e7bf1

Browse files
committed
Logic for FCM error parsing
1 parent 9f46c5e commit 41e7bf1

File tree

6 files changed

+311
-99
lines changed

6 files changed

+311
-99
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseExceptionTest.cs

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17-
using System.Net;
1817
using System.Net.Http;
1918
using System.Net.Sockets;
20-
using System.Text;
2119
using Xunit;
2220

2321
namespace FirebaseAdmin.Tests
@@ -113,98 +111,5 @@ public void UnknownLowLevelError()
113111
Assert.Same(httpError, exception.InnerException);
114112
Assert.Null(exception.HttpResponse);
115113
}
116-
117-
[Fact]
118-
public void PlatformError()
119-
{
120-
var json = @"{
121-
""error"": {
122-
""status"": ""UNAVAILABLE"",
123-
""message"": ""Test error message""
124-
}
125-
}";
126-
var resp = new HttpResponseMessage()
127-
{
128-
StatusCode = HttpStatusCode.ServiceUnavailable,
129-
Content = new StringContent(json, Encoding.UTF8, "application/json"),
130-
};
131-
132-
var handler = new PlatformErrorHandler();
133-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
134-
135-
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
136-
Assert.Equal("Test error message", error.Message);
137-
Assert.Same(resp, error.HttpResponse);
138-
Assert.Null(error.InnerException);
139-
}
140-
141-
[Fact]
142-
public void PlatformErrorWithoutCode()
143-
{
144-
var json = @"{
145-
""error"": {
146-
""message"": ""Test error message""
147-
}
148-
}";
149-
var resp = new HttpResponseMessage()
150-
{
151-
StatusCode = HttpStatusCode.ServiceUnavailable,
152-
Content = new StringContent(json, Encoding.UTF8, "application/json"),
153-
};
154-
155-
var handler = new PlatformErrorHandler();
156-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
157-
158-
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
159-
Assert.Equal("Test error message", error.Message);
160-
Assert.Same(resp, error.HttpResponse);
161-
Assert.Null(error.InnerException);
162-
}
163-
164-
[Fact]
165-
public void PlatformErrorWithoutMessage()
166-
{
167-
var json = @"{
168-
""error"": {
169-
""status"": ""INVALID_ARGUMENT"",
170-
}
171-
}";
172-
var resp = new HttpResponseMessage()
173-
{
174-
StatusCode = HttpStatusCode.ServiceUnavailable,
175-
Content = new StringContent(json, Encoding.UTF8, "application/json"),
176-
};
177-
178-
var handler = new PlatformErrorHandler();
179-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
180-
181-
Assert.Equal(ErrorCode.InvalidArgument, error.ErrorCode);
182-
Assert.Equal(
183-
$"Unexpected HTTP response with status: 503 (ServiceUnavailable)\n{json}",
184-
error.Message);
185-
Assert.Same(resp, error.HttpResponse);
186-
Assert.Null(error.InnerException);
187-
}
188-
189-
[Fact]
190-
public void PlatformErrorWithoutCodeOrMessage()
191-
{
192-
var json = @"{}";
193-
var resp = new HttpResponseMessage()
194-
{
195-
StatusCode = HttpStatusCode.ServiceUnavailable,
196-
Content = new StringContent(json, Encoding.UTF8, "application/json"),
197-
};
198-
199-
var handler = new PlatformErrorHandler();
200-
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
201-
202-
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
203-
Assert.Equal(
204-
"Unexpected HTTP response with status: 503 (ServiceUnavailable)\n{}",
205-
error.Message);
206-
Assert.Same(resp, error.HttpResponse);
207-
Assert.Null(error.InnerException);
208-
}
209114
}
210115
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.Net;
16+
using System.Net.Http;
17+
using System.Text;
18+
using Xunit;
19+
20+
namespace FirebaseAdmin.Messaging.Tests
21+
{
22+
public class MessagingErrorHandlerTest
23+
{
24+
[Fact]
25+
public void PlatformError()
26+
{
27+
var json = @"{
28+
""error"": {
29+
""status"": ""UNAVAILABLE"",
30+
""message"": ""Test error message""
31+
}
32+
}";
33+
var resp = new HttpResponseMessage()
34+
{
35+
StatusCode = HttpStatusCode.ServiceUnavailable,
36+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
37+
};
38+
39+
var handler = new MessagingErrorHandler();
40+
var error = Assert.Throws<FirebaseMessagingException>(() => handler.ThrowIfError(resp, json));
41+
42+
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
43+
Assert.Equal("Test error message", error.Message);
44+
Assert.Null(error.MessagingErrorCode);
45+
Assert.Same(resp, error.HttpResponse);
46+
Assert.Null(error.InnerException);
47+
}
48+
49+
[Fact]
50+
public void PlatformErrorWithMessagingErrorCode()
51+
{
52+
var json = @"{
53+
""error"": {
54+
""status"": ""PERMISSION_DENIED"",
55+
""message"": ""Test error message"",
56+
""details"": [
57+
{
58+
""@type"": ""type.googleapis.com/google.firebase.fcm.v1.FcmError"",
59+
""errorCode"": ""UNREGISTERED""
60+
}
61+
]
62+
}
63+
}";
64+
var resp = new HttpResponseMessage()
65+
{
66+
StatusCode = HttpStatusCode.ServiceUnavailable,
67+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
68+
};
69+
70+
var handler = new MessagingErrorHandler();
71+
var error = Assert.Throws<FirebaseMessagingException>(() => handler.ThrowIfError(resp, json));
72+
73+
Assert.Equal(ErrorCode.PermissionDenied, error.ErrorCode);
74+
Assert.Equal("Test error message", error.Message);
75+
Assert.Equal(MessagingErrorCode.Unregistered, error.MessagingErrorCode);
76+
Assert.Same(resp, error.HttpResponse);
77+
Assert.Null(error.InnerException);
78+
}
79+
80+
[Fact]
81+
public void PlatformErrorWithoutAnyDetails()
82+
{
83+
var json = @"{}";
84+
var resp = new HttpResponseMessage()
85+
{
86+
StatusCode = HttpStatusCode.ServiceUnavailable,
87+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
88+
};
89+
90+
var handler = new MessagingErrorHandler();
91+
var error = Assert.Throws<FirebaseMessagingException>(() => handler.ThrowIfError(resp, json));
92+
93+
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
94+
Assert.Equal(
95+
"Unexpected HTTP response with status: 503 (ServiceUnavailable)\n{}",
96+
error.Message);
97+
Assert.Null(error.MessagingErrorCode);
98+
Assert.Same(resp, error.HttpResponse);
99+
Assert.Null(error.InnerException);
100+
}
101+
}
102+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.Net;
16+
using System.Net.Http;
17+
using System.Text;
18+
using Xunit;
19+
20+
namespace FirebaseAdmin.Tests
21+
{
22+
public class PlatformErrorHandlerTest
23+
{
24+
[Fact]
25+
public void PlatformError()
26+
{
27+
var json = @"{
28+
""error"": {
29+
""status"": ""UNAVAILABLE"",
30+
""message"": ""Test error message""
31+
}
32+
}";
33+
var resp = new HttpResponseMessage()
34+
{
35+
StatusCode = HttpStatusCode.ServiceUnavailable,
36+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
37+
};
38+
39+
var handler = new PlatformErrorHandler();
40+
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
41+
42+
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
43+
Assert.Equal("Test error message", error.Message);
44+
Assert.Same(resp, error.HttpResponse);
45+
Assert.Null(error.InnerException);
46+
}
47+
48+
[Fact]
49+
public void PlatformErrorWithoutCode()
50+
{
51+
var json = @"{
52+
""error"": {
53+
""message"": ""Test error message""
54+
}
55+
}";
56+
var resp = new HttpResponseMessage()
57+
{
58+
StatusCode = HttpStatusCode.ServiceUnavailable,
59+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
60+
};
61+
62+
var handler = new PlatformErrorHandler();
63+
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
64+
65+
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
66+
Assert.Equal("Test error message", error.Message);
67+
Assert.Same(resp, error.HttpResponse);
68+
Assert.Null(error.InnerException);
69+
}
70+
71+
[Fact]
72+
public void PlatformErrorWithoutMessage()
73+
{
74+
var json = @"{
75+
""error"": {
76+
""status"": ""INVALID_ARGUMENT"",
77+
}
78+
}";
79+
var resp = new HttpResponseMessage()
80+
{
81+
StatusCode = HttpStatusCode.ServiceUnavailable,
82+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
83+
};
84+
85+
var handler = new PlatformErrorHandler();
86+
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
87+
88+
Assert.Equal(ErrorCode.InvalidArgument, error.ErrorCode);
89+
Assert.Equal(
90+
$"Unexpected HTTP response with status: 503 (ServiceUnavailable)\n{json}",
91+
error.Message);
92+
Assert.Same(resp, error.HttpResponse);
93+
Assert.Null(error.InnerException);
94+
}
95+
96+
[Fact]
97+
public void PlatformErrorWithoutCodeOrMessage()
98+
{
99+
var json = @"{}";
100+
var resp = new HttpResponseMessage()
101+
{
102+
StatusCode = HttpStatusCode.ServiceUnavailable,
103+
Content = new StringContent(json, Encoding.UTF8, "application/json"),
104+
};
105+
106+
var handler = new PlatformErrorHandler();
107+
var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json));
108+
109+
Assert.Equal(ErrorCode.Unavailable, error.ErrorCode);
110+
Assert.Equal(
111+
"Unexpected HTTP response with status: 503 (ServiceUnavailable)\n{}",
112+
error.Message);
113+
Assert.Same(resp, error.HttpResponse);
114+
Assert.Null(error.InnerException);
115+
}
116+
}
117+
}

FirebaseAdmin/FirebaseAdmin/Messaging/FirebaseMessagingException.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Net.Http;
1617

1718
namespace FirebaseAdmin.Messaging
1819
{
@@ -25,8 +26,9 @@ internal FirebaseMessagingException(
2526
ErrorCode code,
2627
string message,
2728
MessagingErrorCode? fcmCode = null,
28-
Exception inner = null)
29-
: base(code, message, inner)
29+
Exception inner = null,
30+
HttpResponseMessage response = null)
31+
: base(code, message, inner, response)
3032
{
3133
this.MessagingErrorCode = fcmCode;
3234
}

0 commit comments

Comments
 (0)