Skip to content

Commit 56f0948

Browse files
Merge pull request #1031 from appwrite/fix-execption-response
chore: fix response attribute from AppwriteException
2 parents ea2b16f + df64c27 commit 56f0948

File tree

34 files changed

+134
-44
lines changed

34 files changed

+134
-44
lines changed

templates/android/library/src/main/java/io/package/Client.kt.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class Client @JvmOverloads constructor(
490490
body
491491
)
492492
} else {
493-
{{ spec.title | caseUcfirst }}Exception(body, response.code)
493+
{{ spec.title | caseUcfirst }}Exception(body, response.code, "", body)
494494
}
495495
it.cancel(error)
496496
return

templates/apple/Sources/Client.swift.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,24 @@ open class Client {
332332
default:
333333
var message = ""
334334
var type = ""
335+
var responseString = ""
335336

336337
do {
337338
let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any]
338339

339340
message = dict?["message"] as? String ?? response.status.reasonPhrase
340341
type = dict?["type"] as? String ?? ""
342+
responseString = String(decoding: data.readableBytesView, as: UTF8.self)
341343
} catch {
342344
message = data.readString(length: data.readableBytes)!
345+
responseString = message
343346
}
344347

345348
throw {{ spec.title | caseUcfirst }}Error(
346349
message: message,
347350
code: Int(response.status.code),
348-
type: type
351+
type: type,
352+
response: responseString
349353
)
350354
}
351355
}

templates/cli/lib/client.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Client {
158158
globalConfig.setCurrentSession('');
159159
globalConfig.removeSession(current);
160160
}
161-
throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, json);
161+
throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, text);
162162
}
163163

164164
if (responseType === "arraybuffer") {

templates/dart/lib/src/client_mixin.dart.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ mixin ClientMixin {
8484
response['message'],
8585
response['code'],
8686
response['type'],
87-
response,
87+
res.body,
8888
);
8989
} else {
90-
throw {{spec.title | caseUcfirst}}Exception(res.body);
90+
throw {{spec.title | caseUcfirst}}Exception(res.body, res.statusCode, '', res.body);
9191
}
9292
}
9393
dynamic data;

templates/dart/lib/src/exception.dart.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class {{spec.title | caseUcfirst}}Exception implements Exception {
99
/// for more information.
1010
final String? type;
1111
final int? code;
12-
final dynamic response;
12+
final String? response;
1313

1414
/// Initializes an {{spec.title | caseUcfirst}} Exception.
1515
{{spec.title | caseUcfirst}}Exception([this.message = "", this.code, this.type, this.response]);

templates/deno/src/client.ts.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class Client {
113113
} catch (error) {
114114
throw new {{spec.title | caseUcfirst}}Exception(text, response.status, "", text);
115115
}
116-
throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, json);
116+
throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, text);
117117
}
118118

119119
if (responseType === "arraybuffer") {

templates/dotnet/Package/Client.cs.twig

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ namespace {{ spec.title | caseUcfirst }}
230230
var code = (int)response.StatusCode;
231231

232232
if (code >= 400) {
233-
var message = await response.Content.ReadAsStringAsync();
233+
var text = await response.Content.ReadAsStringAsync();
234+
var message = "";
235+
var type = "";
234236

235237
string contentType = string.Empty;
236238
if (response.Content.Headers.TryGetValues("Content-Type", out var contentTypes))
@@ -239,10 +241,13 @@ namespace {{ spec.title | caseUcfirst }}
239241
}
240242

241243
if (contentType.Contains("application/json")) {
242-
message = JObject.Parse(message)["message"]!.ToString();
244+
message = JObject.Parse(text)["message"]!.ToString();
245+
type = JObject.Parse(text)["type"]?.ToString() ?? string.Empty;
246+
} else {
247+
message = text;
243248
}
244249

245-
throw new {{spec.title | caseUcfirst}}Exception(message, code);
250+
throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text);
246251
}
247252

248253
return response.Headers.Location.OriginalString;
@@ -286,13 +291,18 @@ namespace {{ spec.title | caseUcfirst }}
286291
var isJson = contentType.Contains("application/json");
287292

288293
if (code >= 400) {
289-
var message = await response.Content.ReadAsStringAsync();
294+
var text = await response.Content.ReadAsStringAsync();
295+
var message = "";
296+
var type = "";
290297

291298
if (isJson) {
292-
message = JObject.Parse(message)["message"]!.ToString();
299+
message = JObject.Parse(text)["message"]!.ToString();
300+
type = JObject.Parse(text)["type"]?.ToString() ?? string.Empty;
301+
} else {
302+
message = text;
293303
}
294304

295-
throw new {{spec.title | caseUcfirst}}Exception(message, code);
305+
throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text);
296306
}
297307

298308
if (isJson)

templates/flutter/lib/src/client_mixin.dart.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ mixin ClientMixin {
8484
response['message'],
8585
response['code'],
8686
response['type'],
87-
response,
87+
res.body,
8888
);
8989
} else {
90-
throw {{spec.title | caseUcfirst}}Exception(res.body);
90+
throw {{spec.title | caseUcfirst}}Exception(res.body, res.statusCode, '', res.body);
9191
}
9292
}
9393
dynamic data;

templates/go/client.go.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
type {{ spec.title | caseUcfirst }}Error struct {
3333
statusCode int
3434
message string
35+
response string
3536
}
3637

3738
// ClientResponse - represents the client response
@@ -55,6 +56,10 @@ func (ce *{{ spec.title | caseUcfirst }}Error) GetStatusCode() int {
5556
return ce.statusCode
5657
}
5758

59+
func (ce *{{ spec.title | caseUcfirst }}Error) GetResponse() string {
60+
return ce.response
61+
}
62+
5863
// Client is the client struct to access {{ spec.title | caseUcfirst }} services
5964
type Client struct {
6065
Client *http.Client
@@ -377,6 +382,7 @@ func (client *Client) Call(method string, path string, headers map[string]interf
377382
return nil, &{{ spec.title | caseUcfirst }}Error{
378383
statusCode: resp.StatusCode,
379384
message: message,
385+
response: string(responseData),
380386
}
381387
}
382388
return &ClientResponse{
@@ -392,6 +398,7 @@ func (client *Client) Call(method string, path string, headers map[string]interf
392398
return nil, &{{ spec.title | caseUcfirst }}Error{
393399
statusCode: resp.StatusCode,
394400
message: string(responseData),
401+
response: string(responseData),
395402
}
396403
}
397404
return &ClientResponse{

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ class Client @JvmOverloads constructor(
486486
body
487487
)
488488
} else {
489-
{{ spec.title | caseUcfirst }}Exception(body, response.code)
489+
{{ spec.title | caseUcfirst }}Exception(body, response.code, "", body)
490490
}
491491
it.cancel(error)
492492
return
@@ -537,7 +537,7 @@ class Client @JvmOverloads constructor(
537537
body
538538
)
539539
} else {
540-
{{ spec.title | caseUcfirst }}Exception(body, response.code)
540+
{{ spec.title | caseUcfirst }}Exception(body, response.code, "", body)
541541
}
542542
it.cancel(error)
543543
return

0 commit comments

Comments
 (0)