Skip to content

Commit b9edd68

Browse files
authored
Error parsing (#107)
1 parent 5eda228 commit b9edd68

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.9.2
2+
3+
- Add exception parsing from server response
4+
15
# 0.9.1
26

37
- Support passkeys in Flutter Web

lib/src/internal/http/descope_client.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import '/src/internal/others/error.dart';
24
import '/src/sdk/config.dart';
35
import '/src/sdk/sdk.dart';
@@ -361,6 +363,19 @@ class DescopeClient extends HttpClient {
361363
'x-descope-sdk-version': DescopeSdk.version,
362364
};
363365

366+
@override
367+
DescopeException? exceptionFromResponse(String response) {
368+
try {
369+
final json = jsonDecode(response) as Map<String, dynamic>;
370+
var code = json["errorCode"] as String;
371+
var desc = json["errorDescription"] as String?;
372+
var message = json["errorMessage"] as String?;
373+
return DescopeException(code: code, desc: desc ?? "Descope server error", message: message);
374+
} catch (e) {
375+
return null;
376+
}
377+
}
378+
364379
Map<String, String> authorization(String? value) {
365380
return value != null ? {'Authorization': 'Bearer ${config.projectId}:$value'} : {};
366381
}

lib/src/internal/http/http_client.dart

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class HttpClient {
4545

4646
Map<String, String> get defaultHeaders => {};
4747

48+
DescopeException? exceptionFromResponse(String response) {
49+
return null;
50+
}
51+
4852
// Internal
4953

5054
Future<T> call<T>(http.Request request, ResponseDecoder<T> decoder) async {
@@ -90,8 +94,10 @@ class HttpClient {
9094
}
9195

9296
String parseResponse(http.Response response) {
93-
throwErrorIfNeeded(response.statusCode);
94-
return response.body;
97+
if (response.statusCode >= 200 && response.statusCode <= 299) {
98+
return response.body;
99+
}
100+
throw exceptionFromResponse(response.body) ?? generalServerError(response.statusCode);
95101
}
96102
}
97103

@@ -113,32 +119,29 @@ extension CompactMap<T> on Map<String, T?> {
113119

114120
const String invalidResponse = 'The server returned an unexpected response';
115121

116-
void throwErrorIfNeeded(int statusCode) {
117-
final desc = errorDescriptionFromCode(statusCode);
118-
if (desc != null) {
119-
throw InternalErrors.httpError.add(desc: desc);
120-
}
121-
}
122-
123-
String? errorDescriptionFromCode(int statusCode) {
124-
if (statusCode >= 200 && statusCode <= 299) {
125-
return null;
126-
}
122+
DescopeException generalServerError(int statusCode) {
123+
String desc;
127124
switch (statusCode) {
128125
case 400:
129-
return 'The request was invalid';
126+
desc = 'The request was invalid';
127+
break;
130128
case 401:
131-
return 'The request was unauthorized';
129+
desc = 'The request was unauthorized';
130+
break;
132131
case 403:
133-
return 'The request was forbidden';
132+
desc = 'The request was forbidden';
133+
break;
134134
case 404:
135-
return 'The resource was not found';
135+
desc = 'The resource was not found';
136+
break;
136137
case 500:
137138
case 503:
138-
return "The server failed with status code $statusCode";
139+
desc = "The server failed with status code $statusCode";
140+
break;
139141
default:
140-
return statusCode >= 500 ? 'The server was unreachable' : "The server returned status code $statusCode";
142+
desc = statusCode >= 500 ? 'The server was unreachable' : "The server returned status code $statusCode";
141143
}
144+
return InternalErrors.httpError.add(desc: desc);
142145
}
143146

144147
// Default Network Client

lib/src/sdk/sdk.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DescopeSdk {
2222
static const name = 'DescopeFlutter';
2323

2424
/// The Descope SDK version
25-
static const version = '0.9.1';
25+
static const version = '0.9.2';
2626

2727
/// The configuration of the [DescopeSdk] instance.
2828
final DescopeConfig config;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: descope
22
description: A Flutter package for working with the Descope API.
3-
version: 0.9.1
3+
version: 0.9.2
44
homepage: https://www.descope.com
55
repository: https://github.com/descope/descope-flutter
66
issue_tracker: https://github.com/descope/descope-flutter/issues

0 commit comments

Comments
 (0)