From 1ec451908eed839732b0126a54813a8f2998a262 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 29 Jan 2025 10:47:36 +0000 Subject: [PATCH 01/10] chore: fixed response types across sdks --- templates/apple/Sources/Client.swift.twig | 3 ++- templates/cli/lib/client.js.twig | 2 +- templates/dart/lib/src/client_mixin.dart.twig | 2 +- templates/deno/src/client.ts.twig | 2 +- templates/dotnet/Package/Client.cs.twig | 16 +++++++++++----- templates/flutter/lib/src/client_mixin.dart.twig | 2 +- templates/node/src/client.ts.twig | 5 +++-- templates/python/package/client.py.twig | 2 +- templates/react-native/src/client.ts.twig | 5 +++-- templates/ruby/lib/container/client.rb.twig | 2 +- templates/swift/Sources/Client.swift.twig | 6 ++++-- templates/swift/Sources/Models/Error.swift.twig | 4 +++- templates/web/src/client.ts.twig | 5 +++-- 13 files changed, 35 insertions(+), 21 deletions(-) diff --git a/templates/apple/Sources/Client.swift.twig b/templates/apple/Sources/Client.swift.twig index 93fa30c0f..397ef5a5e 100644 --- a/templates/apple/Sources/Client.swift.twig +++ b/templates/apple/Sources/Client.swift.twig @@ -346,7 +346,8 @@ open class Client { throw {{ spec.title | caseUcfirst }}Error( message: message, code: Int(response.status.code), - type: type + type: type, + data ) } } diff --git a/templates/cli/lib/client.js.twig b/templates/cli/lib/client.js.twig index 34faaf611..082b4de09 100644 --- a/templates/cli/lib/client.js.twig +++ b/templates/cli/lib/client.js.twig @@ -158,7 +158,7 @@ class Client { globalConfig.setCurrentSession(''); globalConfig.removeSession(current); } - throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, json); + throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, text); } if (responseType === "arraybuffer") { diff --git a/templates/dart/lib/src/client_mixin.dart.twig b/templates/dart/lib/src/client_mixin.dart.twig index dacaa01b4..0a8ad62a7 100644 --- a/templates/dart/lib/src/client_mixin.dart.twig +++ b/templates/dart/lib/src/client_mixin.dart.twig @@ -81,7 +81,7 @@ class ClientMixin { response['message'], response['code'], response['type'], - response, + res.body, ); } else { throw {{spec.title | caseUcfirst}}Exception(res.body); diff --git a/templates/deno/src/client.ts.twig b/templates/deno/src/client.ts.twig index 754a6b872..891fd8ce6 100644 --- a/templates/deno/src/client.ts.twig +++ b/templates/deno/src/client.ts.twig @@ -113,7 +113,7 @@ export class Client { } catch (error) { throw new {{spec.title | caseUcfirst}}Exception(text, response.status, "", text); } - throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, json); + throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, text); } if (responseType === "arraybuffer") { diff --git a/templates/dotnet/Package/Client.cs.twig b/templates/dotnet/Package/Client.cs.twig index 86df8976b..34e272f53 100644 --- a/templates/dotnet/Package/Client.cs.twig +++ b/templates/dotnet/Package/Client.cs.twig @@ -230,7 +230,9 @@ namespace {{ spec.title | caseUcfirst }} var code = (int)response.StatusCode; if (code >= 400) { - var message = await response.Content.ReadAsStringAsync(); + var text = await response.Content.ReadAsStringAsync(); + var message = ""; + var type = ""; string contentType = string.Empty; if (response.Content.Headers.TryGetValues("Content-Type", out var contentTypes)) @@ -239,10 +241,11 @@ namespace {{ spec.title | caseUcfirst }} } if (contentType.Contains("application/json")) { - message = JObject.Parse(message)["message"]!.ToString(); + message = JObject.Parse(text)["message"]!.ToString(); + type = JObject.Parse(text)["type"]!.ToString(); } - throw new {{spec.title | caseUcfirst}}Exception(message, code); + throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text); } return response.Headers.Location.OriginalString; @@ -286,13 +289,16 @@ namespace {{ spec.title | caseUcfirst }} var isJson = contentType.Contains("application/json"); if (code >= 400) { - var message = await response.Content.ReadAsStringAsync(); + var text = await response.Content.ReadAsStringAsync(); + var message = ""; + var type = ""; if (isJson) { message = JObject.Parse(message)["message"]!.ToString(); + type = JObject.Parse(message)["type"]!.ToString(); } - throw new {{spec.title | caseUcfirst}}Exception(message, code); + throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text); } if (isJson) diff --git a/templates/flutter/lib/src/client_mixin.dart.twig b/templates/flutter/lib/src/client_mixin.dart.twig index dacaa01b4..0a8ad62a7 100644 --- a/templates/flutter/lib/src/client_mixin.dart.twig +++ b/templates/flutter/lib/src/client_mixin.dart.twig @@ -81,7 +81,7 @@ class ClientMixin { response['message'], response['code'], response['type'], - response, + res.body, ); } else { throw {{spec.title | caseUcfirst}}Exception(res.body); diff --git a/templates/node/src/client.ts.twig b/templates/node/src/client.ts.twig index 7dffac1cf..8f7bfca36 100644 --- a/templates/node/src/client.ts.twig +++ b/templates/node/src/client.ts.twig @@ -265,6 +265,7 @@ class Client { let data: any = null; const response = await fetch(uri, options); + const text = await response.text(); const warnings = response.headers.get('x-{{ spec.title | lower }}-warning'); if (warnings) { @@ -277,12 +278,12 @@ class Client { data = await response.arrayBuffer(); } else { data = { - message: await response.text() + message: text }; } if (400 <= response.status) { - throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, data); + throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, text); } return data; diff --git a/templates/python/package/client.py.twig b/templates/python/package/client.py.twig index 1b835fe80..6dc83b2ff 100644 --- a/templates/python/package/client.py.twig +++ b/templates/python/package/client.py.twig @@ -109,7 +109,7 @@ class Client: if response != None: content_type = response.headers['Content-Type'] if content_type.startswith('application/json'): - raise {{spec.title | caseUcfirst}}Exception(response.json()['message'], response.status_code, response.json().get('type'), response.json()) + raise {{spec.title | caseUcfirst}}Exception(response.json()['message'], response.status_code, response.json().get('type'), response.text) else: raise {{spec.title | caseUcfirst}}Exception(response.text, response.status_code) else: diff --git a/templates/react-native/src/client.ts.twig b/templates/react-native/src/client.ts.twig index b8db9546a..3be5e7702 100644 --- a/templates/react-native/src/client.ts.twig +++ b/templates/react-native/src/client.ts.twig @@ -415,6 +415,7 @@ class Client { try { let data = null; const response = await fetch(url.toString(), options); + const text = await response.text() const warnings = response.headers.get('x-{{ spec.title | lower }}-warning'); if (warnings) { @@ -425,12 +426,12 @@ class Client { data = await response.json(); } else { data = { - message: await response.text() + message: text }; } if (400 <= response.status) { - throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, data); + throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, text); } const cookieFallback = response.headers.get('X-Fallback-Cookies'); diff --git a/templates/ruby/lib/container/client.rb.twig b/templates/ruby/lib/container/client.rb.twig index 9e5bb98a5..d4934ebd1 100644 --- a/templates/ruby/lib/container/client.rb.twig +++ b/templates/ruby/lib/container/client.rb.twig @@ -252,7 +252,7 @@ module {{ spec.title | caseUcfirst }} end if response.code.to_i >= 400 - raise {{spec.title | caseUcfirst}}::Exception.new(result['message'], result['status'], result['type'], result) + raise {{spec.title | caseUcfirst}}::Exception.new(result['message'], result['status'], result['type'], response) end unless response_type.respond_to?("from") diff --git a/templates/swift/Sources/Client.swift.twig b/templates/swift/Sources/Client.swift.twig index 011d5545d..aef6d8a2f 100644 --- a/templates/swift/Sources/Client.swift.twig +++ b/templates/swift/Sources/Client.swift.twig @@ -285,7 +285,8 @@ open class Client { throw {{ spec.title | caseUcfirst }}Error( message: message, code: Int(response.status.code), - type: type + type: type, + data ) } @@ -382,7 +383,8 @@ open class Client { throw {{ spec.title | caseUcfirst }}Error( message: message, code: Int(response.status.code), - type: type + type: type, + data ) } } diff --git a/templates/swift/Sources/Models/Error.swift.twig b/templates/swift/Sources/Models/Error.swift.twig index 046ee2289..955f37d90 100644 --- a/templates/swift/Sources/Models/Error.swift.twig +++ b/templates/swift/Sources/Models/Error.swift.twig @@ -5,11 +5,13 @@ open class {{ spec.title | caseUcfirst}}Error : Swift.Error, Decodable { public let message: String public let code: Int? public let type: String? + public let response: String? - init(message: String, code: Int? = nil, type: String? = nil) { + init(message: String, code: Int? = nil, type: String? = nil, response: String? = nil) { self.message = message self.code = code self.type = type + self.response = response } } diff --git a/templates/web/src/client.ts.twig b/templates/web/src/client.ts.twig index e5617851c..165bb9d97 100644 --- a/templates/web/src/client.ts.twig +++ b/templates/web/src/client.ts.twig @@ -671,6 +671,7 @@ class Client { let data: any = null; const response = await fetch(uri, options); + const text = await response.text(); const warnings = response.headers.get('x-{{ spec.title | lower }}-warning'); if (warnings) { @@ -683,12 +684,12 @@ class Client { data = await response.arrayBuffer(); } else { data = { - message: await response.text() + message: text }; } if (400 <= response.status) { - throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, data); + throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, text); } const cookieFallback = response.headers.get('X-Fallback-Cookies'); From 1959f046d1363b4b2e9dbaed1329e819d3b5c302 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 29 Jan 2025 12:21:35 +0000 Subject: [PATCH 02/10] fix: tests in dotnet, swift and node --- templates/apple/Sources/Client.swift.twig | 2 +- templates/dotnet/Package/Client.cs.twig | 10 +++++++--- templates/node/src/client.ts.twig | 4 +++- templates/swift/Sources/Client.swift.twig | 4 ++-- templates/web/src/client.ts.twig | 4 +++- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/templates/apple/Sources/Client.swift.twig b/templates/apple/Sources/Client.swift.twig index 397ef5a5e..92e8320f5 100644 --- a/templates/apple/Sources/Client.swift.twig +++ b/templates/apple/Sources/Client.swift.twig @@ -347,7 +347,7 @@ open class Client { message: message, code: Int(response.status.code), type: type, - data + response: String(decoding: data.readableBytesView, as: UTF8.self) ) } } diff --git a/templates/dotnet/Package/Client.cs.twig b/templates/dotnet/Package/Client.cs.twig index 34e272f53..a6b906f24 100644 --- a/templates/dotnet/Package/Client.cs.twig +++ b/templates/dotnet/Package/Client.cs.twig @@ -242,7 +242,9 @@ namespace {{ spec.title | caseUcfirst }} if (contentType.Contains("application/json")) { message = JObject.Parse(text)["message"]!.ToString(); - type = JObject.Parse(text)["type"]!.ToString(); + type = JObject.Parse(text)["type"]?.ToString() ?? string.Empty; + } else { + message = text; } throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text); @@ -294,8 +296,10 @@ namespace {{ spec.title | caseUcfirst }} var type = ""; if (isJson) { - message = JObject.Parse(message)["message"]!.ToString(); - type = JObject.Parse(message)["type"]!.ToString(); + message = JObject.Parse(text)["message"]!.ToString(); + type = JObject.Parse(text)["type"]?.ToString() ?? string.Empty; + } else { + message = text; } throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text); diff --git a/templates/node/src/client.ts.twig b/templates/node/src/client.ts.twig index 8f7bfca36..c4b56f534 100644 --- a/templates/node/src/client.ts.twig +++ b/templates/node/src/client.ts.twig @@ -263,9 +263,9 @@ class Client { const { uri, options } = this.prepareRequest(method, url, headers, params); let data: any = null; + let text: string = ''; const response = await fetch(uri, options); - const text = await response.text(); const warnings = response.headers.get('x-{{ spec.title | lower }}-warning'); if (warnings) { @@ -274,9 +274,11 @@ class Client { if (response.headers.get('content-type')?.includes('application/json')) { data = await response.json(); + text = JSON.stringify(data); } else if (responseType === 'arrayBuffer') { data = await response.arrayBuffer(); } else { + text = await response.text(); data = { message: text }; diff --git a/templates/swift/Sources/Client.swift.twig b/templates/swift/Sources/Client.swift.twig index aef6d8a2f..c8e60cedd 100644 --- a/templates/swift/Sources/Client.swift.twig +++ b/templates/swift/Sources/Client.swift.twig @@ -286,7 +286,7 @@ open class Client { message: message, code: Int(response.status.code), type: type, - data + response: String(decoding: data.readableBytesView, as: UTF8.self) ) } @@ -384,7 +384,7 @@ open class Client { message: message, code: Int(response.status.code), type: type, - data + response: String(decoding: data.readableBytesView, as: UTF8.self) ) } } diff --git a/templates/web/src/client.ts.twig b/templates/web/src/client.ts.twig index 165bb9d97..f85e065c2 100644 --- a/templates/web/src/client.ts.twig +++ b/templates/web/src/client.ts.twig @@ -669,9 +669,9 @@ class Client { const { uri, options } = this.prepareRequest(method, url, headers, params); let data: any = null; + let text: string = ''; const response = await fetch(uri, options); - const text = await response.text(); const warnings = response.headers.get('x-{{ spec.title | lower }}-warning'); if (warnings) { @@ -680,9 +680,11 @@ class Client { if (response.headers.get('content-type')?.includes('application/json')) { data = await response.json(); + text = JSON.stringify(data); } else if (responseType === 'arrayBuffer') { data = await response.arrayBuffer(); } else { + text = await response.text(); data = { message: text }; From a8259d77929099c5777f7becfd58715663802400 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 29 Jan 2025 13:28:52 +0000 Subject: [PATCH 03/10] chore: adding tests for exception responses, fix swift --- templates/swift/Sources/Client.swift.twig | 10 ++++++++-- templates/swift/Sources/Models/Error.swift.twig | 4 ++-- tests/Base.php | 7 +++++++ tests/languages/swift/Tests.swift | 15 +++++++++------ tests/languages/web/node.js | 3 +++ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/templates/swift/Sources/Client.swift.twig b/templates/swift/Sources/Client.swift.twig index c8e60cedd..da5585920 100644 --- a/templates/swift/Sources/Client.swift.twig +++ b/templates/swift/Sources/Client.swift.twig @@ -272,21 +272,24 @@ open class Client { var message = "" var data = try await response.body.collect(upTo: Int.max) var type = "" + var responseString = "" do { let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any] message = dict?["message"] as? String ?? response.status.reasonPhrase type = dict?["type"] as? String ?? "" + responseString = String(decoding: data.readableBytesView, as: UTF8.self) } catch { message = data.readString(length: data.readableBytes)! + responseString = message } throw {{ spec.title | caseUcfirst }}Error( message: message, code: Int(response.status.code), type: type, - response: String(decoding: data.readableBytesView, as: UTF8.self) + response: responseString ) } @@ -370,21 +373,24 @@ open class Client { default: var message = "" var type = "" + var responseString = "" do { let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any] message = dict?["message"] as? String ?? response.status.reasonPhrase type = dict?["type"] as? String ?? "" + responseString = String(decoding: data.readableBytesView, as: UTF8.self) } catch { message = data.readString(length: data.readableBytes)! + responseString = message } throw {{ spec.title | caseUcfirst }}Error( message: message, code: Int(response.status.code), type: type, - response: String(decoding: data.readableBytesView, as: UTF8.self) + response: responseString ) } } diff --git a/templates/swift/Sources/Models/Error.swift.twig b/templates/swift/Sources/Models/Error.swift.twig index 955f37d90..26e70e9c1 100644 --- a/templates/swift/Sources/Models/Error.swift.twig +++ b/templates/swift/Sources/Models/Error.swift.twig @@ -5,9 +5,9 @@ open class {{ spec.title | caseUcfirst}}Error : Swift.Error, Decodable { public let message: String public let code: Int? public let type: String? - public let response: String? + public let response: String - init(message: String, code: Int? = nil, type: String? = nil, response: String? = nil) { + init(message: String, code: Int? = nil, type: String? = nil, response: String = "") { self.message = message self.code = code self.type = type diff --git a/tests/Base.php b/tests/Base.php index 86422c7b2..19d63ff84 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -68,9 +68,16 @@ abstract class Base extends TestCase 'POST:/v1/mock/tests/general/upload:passed', ]; + /** + * 'Mock 400 error' -> message + * '{"message":"Mock 400 error","code":400}' -> response + */ protected const EXCEPTION_RESPONSES = [ 'Mock 400 error', + '{"message":"Mock 400 error","code":400}', 'Mock 500 error', + '{"message":"Mock 500 error","code":500}', + 'This is a text error', 'This is a text error', ]; diff --git a/tests/languages/swift/Tests.swift b/tests/languages/swift/Tests.swift index 7269011bd..7b88fc47d 100644 --- a/tests/languages/swift/Tests.swift +++ b/tests/languages/swift/Tests.swift @@ -118,20 +118,23 @@ class Tests: XCTestCase { do { try await general.error400() - } catch { - print(error.localizedDescription) + } catch let error as AppwriteError { + print(error.message) + print(error.response) } do { try await general.error500() - } catch { - print(error.localizedDescription) + } catch let error as AppwriteError { + print(error.message) + print(error.response) } do { try await general.error502() - } catch { - print(error.localizedDescription) + } catch let error as AppwriteError { + print(error.message) + print(error.response) } try! await general.empty() diff --git a/tests/languages/web/node.js b/tests/languages/web/node.js index 508a05d39..fa2789b16 100644 --- a/tests/languages/web/node.js +++ b/tests/languages/web/node.js @@ -67,16 +67,19 @@ async function start() { response = await general.error400(); } catch(error) { console.log(error.message); + console.log(error.response); } try { response = await general.error500(); } catch(error) { console.log(error.message); + console.log(error.response); } try { response = await general.error502(); } catch (error) { console.log(error.message); + console.log(error.response); } console.log('WS:/v1/realtime:passed'); // Skip realtime test on Node.js From ba3a9ada0183b24b4b586973a05a7838a99c07c1 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 29 Jan 2025 13:46:26 +0000 Subject: [PATCH 04/10] chore: added response logging to tests --- templates/apple/Sources/Client.swift.twig | 5 ++++- templates/php/src/Client.php.twig | 2 +- templates/python/package/client.py.twig | 2 +- templates/ruby/lib/container/client.rb.twig | 6 +++--- tests/languages/android/Tests.kt | 3 +++ tests/languages/dart/tests.dart | 3 +++ tests/languages/deno/tests.ts | 3 +++ tests/languages/dotnet/Tests.cs | 3 +++ tests/languages/flutter/tests.dart | 3 +++ tests/languages/kotlin/Tests.kt | 3 +++ tests/languages/node/test.js | 3 +++ tests/languages/php/test.php | 3 +++ tests/languages/python/tests.py | 3 +++ tests/languages/ruby/tests.rb | 3 +++ 14 files changed, 39 insertions(+), 6 deletions(-) diff --git a/templates/apple/Sources/Client.swift.twig b/templates/apple/Sources/Client.swift.twig index 92e8320f5..eb13593f8 100644 --- a/templates/apple/Sources/Client.swift.twig +++ b/templates/apple/Sources/Client.swift.twig @@ -333,21 +333,24 @@ open class Client { default: var message = "" var type = "" + var responseString = "" do { let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any] message = dict?["message"] as? String ?? response.status.reasonPhrase type = dict?["type"] as? String ?? "" + responseString = String(decoding: data.readableBytesView, as: UTF8.self) } catch { message = data.readString(length: data.readableBytes)! + responseString = message } throw {{ spec.title | caseUcfirst }}Error( message: message, code: Int(response.status.code), type: type, - response: String(decoding: data.readableBytesView, as: UTF8.self) + response: responseString ) } } diff --git a/templates/php/src/Client.php.twig b/templates/php/src/Client.php.twig index 894866571..fb61d334c 100644 --- a/templates/php/src/Client.php.twig +++ b/templates/php/src/Client.php.twig @@ -206,7 +206,7 @@ class Client if(is_array($responseBody)) { throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', json_encode($responseBody)); } else { - throw new {{spec.title | caseUcfirst}}Exception($responseBody, $responseStatus); + throw new {{spec.title | caseUcfirst}}Exception($responseBody, $responseStatus, '', $responseBody); } } diff --git a/templates/python/package/client.py.twig b/templates/python/package/client.py.twig index 6dc83b2ff..b05b069c5 100644 --- a/templates/python/package/client.py.twig +++ b/templates/python/package/client.py.twig @@ -111,7 +111,7 @@ class Client: if content_type.startswith('application/json'): raise {{spec.title | caseUcfirst}}Exception(response.json()['message'], response.status_code, response.json().get('type'), response.text) else: - raise {{spec.title | caseUcfirst}}Exception(response.text, response.status_code) + raise {{spec.title | caseUcfirst}}Exception(response.text, response.status_code, None, response.text) else: raise {{spec.title | caseUcfirst}}Exception(e) diff --git a/templates/ruby/lib/container/client.rb.twig b/templates/ruby/lib/container/client.rb.twig index d4934ebd1..19ff43d41 100644 --- a/templates/ruby/lib/container/client.rb.twig +++ b/templates/ruby/lib/container/client.rb.twig @@ -248,11 +248,11 @@ module {{ spec.title | caseUcfirst }} begin result = JSON.parse(response.body) rescue JSON::ParserError => e - raise {{spec.title | caseUcfirst}}::Exception.new(response.body, response.code, nil, response) + raise {{spec.title | caseUcfirst}}::Exception.new(response.body, response.code, nil, response.body) end if response.code.to_i >= 400 - raise {{spec.title | caseUcfirst}}::Exception.new(result['message'], result['status'], result['type'], response) + raise {{spec.title | caseUcfirst}}::Exception.new(result['message'], result['status'], result['type'], response.body) end unless response_type.respond_to?("from") @@ -263,7 +263,7 @@ module {{ spec.title | caseUcfirst }} end if response.code.to_i >= 400 - raise {{spec.title | caseUcfirst}}::Exception.new(response.body, response.code, response) + raise {{spec.title | caseUcfirst}}::Exception.new(response.body, response.code, response, response.body) end if response.respond_to?("body_permitted?") diff --git a/tests/languages/android/Tests.kt b/tests/languages/android/Tests.kt index 3d9d7e985..8fe090e7b 100644 --- a/tests/languages/android/Tests.kt +++ b/tests/languages/android/Tests.kt @@ -152,18 +152,21 @@ class ServiceTest { general.error400() } catch (e: AppwriteException) { writeToFile(e.message) + writeToFile(e.response) } try { general.error500() } catch (e: AppwriteException) { writeToFile(e.message) + writeToFile(e.response) } try { general.error502() } catch (e: AppwriteException) { writeToFile(e.message) + writeToFile(e.response) } delay(5000) diff --git a/tests/languages/dart/tests.dart b/tests/languages/dart/tests.dart index 32ab2cd91..1fd67a85c 100644 --- a/tests/languages/dart/tests.dart +++ b/tests/languages/dart/tests.dart @@ -92,18 +92,21 @@ void main() async { await general.error400(); } on AppwriteException catch (e) { print(e.message); + print(e.response); } try { await general.error500(); } on AppwriteException catch (e) { print(e.message); + print(e.response); } try { await general.error502(); } on AppwriteException catch (e) { print(e.message); + print(e.response); } // response = await general.setCookie(); diff --git a/tests/languages/deno/tests.ts b/tests/languages/deno/tests.ts index 9afe1c747..0f4e84b96 100644 --- a/tests/languages/deno/tests.ts +++ b/tests/languages/deno/tests.ts @@ -118,18 +118,21 @@ async function start() { response = await general.error400(); } catch (error) { console.log(error.message); + console.log(error.response); } try { response = await general.error500(); } catch (error) { console.log(error.message); + console.log(error.response); } try { response = await general.error502(); } catch (error) { console.log(error.message); + console.log(error.response); } await general.empty(); diff --git a/tests/languages/dotnet/Tests.cs b/tests/languages/dotnet/Tests.cs index 41fa28886..6f9ce5361 100644 --- a/tests/languages/dotnet/Tests.cs +++ b/tests/languages/dotnet/Tests.cs @@ -91,6 +91,7 @@ public async Task Test1() catch (AppwriteException e) { TestContext.WriteLine(e.Message); + TestContext.WriteLine(e.Response); } try @@ -100,6 +101,7 @@ public async Task Test1() catch (AppwriteException e) { TestContext.WriteLine(e.Message); + TestContext.WriteLine(e.Response); } try @@ -109,6 +111,7 @@ public async Task Test1() catch (AppwriteException e) { TestContext.WriteLine(e.Message); + TestContext.WriteLine(e.Response); } await general.Empty(); diff --git a/tests/languages/flutter/tests.dart b/tests/languages/flutter/tests.dart index f7b27a4c8..f1188077c 100644 --- a/tests/languages/flutter/tests.dart +++ b/tests/languages/flutter/tests.dart @@ -121,18 +121,21 @@ void main() async { await general.error400(); } on AppwriteException catch (e) { print(e.message); + print(e.response); } try { await general.error500(); } on AppwriteException catch (e) { print(e.message); + print(e.response); } try { await general.error502(); } on AppwriteException catch (e) { print(e.message); + print(e.response); } rtsub.stream.listen((message) { diff --git a/tests/languages/kotlin/Tests.kt b/tests/languages/kotlin/Tests.kt index 42145de72..15a5e3325 100644 --- a/tests/languages/kotlin/Tests.kt +++ b/tests/languages/kotlin/Tests.kt @@ -119,18 +119,21 @@ class ServiceTest { general.error400() } catch (e: AppwriteException) { writeToFile(e.message) + writeToFile(e.response) } try { general.error500() } catch (e: AppwriteException) { writeToFile(e.message) + writeToFile(e.response) } try { general.error502() } catch (e: AppwriteException) { writeToFile(e.message) + writeToFile(e.response) } general.empty() diff --git a/tests/languages/node/test.js b/tests/languages/node/test.js index b71a75842..92ed78da8 100644 --- a/tests/languages/node/test.js +++ b/tests/languages/node/test.js @@ -92,18 +92,21 @@ async function start() { response = await general.error400(); } catch(error) { console.log(error.message); + console.log(error.response); } try { response = await general.error500(); } catch(error) { console.log(error.message); + console.log(error.response); } try { response = await general.error502(); } catch(error) { console.log(error.message); + console.log(error.response); } await general.empty(); diff --git a/tests/languages/php/test.php b/tests/languages/php/test.php index f182257b5..cc5321660 100644 --- a/tests/languages/php/test.php +++ b/tests/languages/php/test.php @@ -93,18 +93,21 @@ $response = $general->error400(); } catch (AppwriteException $e) { echo "{$e->getMessage()}\n"; + echo "{$e->getResponse()}\n"; } try { $response = $general->error500(); } catch (AppwriteException $e) { echo "{$e->getMessage()}\n"; + echo "{$e->getResponse()}\n"; } try { $response = $general->error502(); } catch (AppwriteException $e) { echo "{$e->getMessage()}\n"; + echo "{$e->getResponse()}\n"; } $general->empty(); diff --git a/tests/languages/python/tests.py b/tests/languages/python/tests.py index 789373789..87cfc341e 100644 --- a/tests/languages/python/tests.py +++ b/tests/languages/python/tests.py @@ -82,16 +82,19 @@ response = general.error400() except AppwriteException as e: print(e.message) + print(e.response) try: response = general.error500() except AppwriteException as e: print(e.message) + print(e.response) try: response = general.error502() except AppwriteException as e: print(e.message) + print(e.response) general.empty() diff --git a/tests/languages/ruby/tests.rb b/tests/languages/ruby/tests.rb index ffff2d137..355904859 100644 --- a/tests/languages/ruby/tests.rb +++ b/tests/languages/ruby/tests.rb @@ -90,18 +90,21 @@ general.error400() rescue Exception => error puts error.message + puts error.response end begin general.error500() rescue Exception => error puts error.message + puts error.response end begin general.error502() rescue Exception => error puts error.message + puts error.response end general.empty() From 256f74cbd678e05e511ce53dd71363989b0acc55 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 29 Jan 2025 13:57:57 +0000 Subject: [PATCH 05/10] fix: tests for web, add response in just text errors --- .../android/library/src/main/java/io/package/Client.kt.twig | 2 +- templates/dart/lib/src/client_mixin.dart.twig | 2 +- templates/flutter/lib/src/client_mixin.dart.twig | 2 +- templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig | 2 +- tests/languages/web/index.html | 3 +++ 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/templates/android/library/src/main/java/io/package/Client.kt.twig b/templates/android/library/src/main/java/io/package/Client.kt.twig index cddb4007b..24fde7fef 100644 --- a/templates/android/library/src/main/java/io/package/Client.kt.twig +++ b/templates/android/library/src/main/java/io/package/Client.kt.twig @@ -490,7 +490,7 @@ class Client @JvmOverloads constructor( body ) } else { - {{ spec.title | caseUcfirst }}Exception(body, response.code) + {{ spec.title | caseUcfirst }}Exception(body, response.code, '', body) } it.cancel(error) return diff --git a/templates/dart/lib/src/client_mixin.dart.twig b/templates/dart/lib/src/client_mixin.dart.twig index 0a8ad62a7..a8eeab006 100644 --- a/templates/dart/lib/src/client_mixin.dart.twig +++ b/templates/dart/lib/src/client_mixin.dart.twig @@ -84,7 +84,7 @@ class ClientMixin { res.body, ); } else { - throw {{spec.title | caseUcfirst}}Exception(res.body); + throw {{spec.title | caseUcfirst}}Exception(res.body, res.statusCode, '', res.body); } } dynamic data; diff --git a/templates/flutter/lib/src/client_mixin.dart.twig b/templates/flutter/lib/src/client_mixin.dart.twig index 0a8ad62a7..a8eeab006 100644 --- a/templates/flutter/lib/src/client_mixin.dart.twig +++ b/templates/flutter/lib/src/client_mixin.dart.twig @@ -84,7 +84,7 @@ class ClientMixin { res.body, ); } else { - throw {{spec.title | caseUcfirst}}Exception(res.body); + throw {{spec.title | caseUcfirst}}Exception(res.body, res.statusCode, '', res.body); } } dynamic data; diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig index afd9916ff..490256a44 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig @@ -486,7 +486,7 @@ class Client @JvmOverloads constructor( body ) } else { - {{ spec.title | caseUcfirst }}Exception(body, response.code) + {{ spec.title | caseUcfirst }}Exception(body, response.code, '', body) } it.cancel(error) return diff --git a/tests/languages/web/index.html b/tests/languages/web/index.html index 36ac7254c..83fd254e4 100644 --- a/tests/languages/web/index.html +++ b/tests/languages/web/index.html @@ -109,18 +109,21 @@ response = await general.error400(); } catch (error) { console.log(error.message); + console.log(error.response); } try { response = await general.error500(); } catch (error) { console.log(error.message); + console.log(error.response); } try { response = await general.error502(); } catch (error) { console.log(error.message); + console.log(error.response); } const delay = ms => new Promise(res => setTimeout(res, ms)); From 43033840fcb64774fc4ab4be92ecf631ce7ecf3a Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 29 Jan 2025 14:26:17 +0000 Subject: [PATCH 06/10] fix: tests for apple, kotlin and go --- .../src/main/java/io/package/Client.kt.twig | 2 +- templates/go/client.go.twig | 7 +++++++ .../src/main/kotlin/io/appwrite/Client.kt.twig | 4 ++-- tests/languages/apple/Tests.swift | 15 +++++++++------ tests/languages/go/tests.go | 3 +++ 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/templates/android/library/src/main/java/io/package/Client.kt.twig b/templates/android/library/src/main/java/io/package/Client.kt.twig index 24fde7fef..564d3600f 100644 --- a/templates/android/library/src/main/java/io/package/Client.kt.twig +++ b/templates/android/library/src/main/java/io/package/Client.kt.twig @@ -490,7 +490,7 @@ class Client @JvmOverloads constructor( body ) } else { - {{ spec.title | caseUcfirst }}Exception(body, response.code, '', body) + {{ spec.title | caseUcfirst }}Exception(body, response.code, "", body) } it.cancel(error) return diff --git a/templates/go/client.go.twig b/templates/go/client.go.twig index e268d26b3..b079bf199 100644 --- a/templates/go/client.go.twig +++ b/templates/go/client.go.twig @@ -32,6 +32,7 @@ const ( type {{ spec.title | caseUcfirst }}Error struct { statusCode int message string + response string } // ClientResponse - represents the client response @@ -55,6 +56,10 @@ func (ce *{{ spec.title | caseUcfirst }}Error) GetStatusCode() int { return ce.statusCode } +func (ce *{{ spec.title | caseUcfirst }}Error) GetResponse() string { + return ce.response +} + // Client is the client struct to access {{ spec.title | caseUcfirst }} services type Client struct { Client *http.Client @@ -377,6 +382,7 @@ func (client *Client) Call(method string, path string, headers map[string]interf return nil, &{{ spec.title | caseUcfirst }}Error{ statusCode: resp.StatusCode, message: message, + response: string(responseData), } } return &ClientResponse{ @@ -392,6 +398,7 @@ func (client *Client) Call(method string, path string, headers map[string]interf return nil, &{{ spec.title | caseUcfirst }}Error{ statusCode: resp.StatusCode, message: string(responseData), + response: string(responseData), } } return &ClientResponse{ diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig index 490256a44..6e7602bde 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig @@ -486,7 +486,7 @@ class Client @JvmOverloads constructor( body ) } else { - {{ spec.title | caseUcfirst }}Exception(body, response.code, '', body) + {{ spec.title | caseUcfirst }}Exception(body, response.code, "", body) } it.cancel(error) return @@ -537,7 +537,7 @@ class Client @JvmOverloads constructor( body ) } else { - {{ spec.title | caseUcfirst }}Exception(body, response.code) + {{ spec.title | caseUcfirst }}Exception(body, response.code, "", body) } it.cancel(error) return diff --git a/tests/languages/apple/Tests.swift b/tests/languages/apple/Tests.swift index 9ac970e3b..61fca4416 100644 --- a/tests/languages/apple/Tests.swift +++ b/tests/languages/apple/Tests.swift @@ -128,20 +128,23 @@ class Tests: XCTestCase { do { try await general.error400() - } catch { - print(error.localizedDescription) + } catch let error as AppwriteError { + print(error.message) + print(error.response) } do { try await general.error500() - } catch { - print(error.localizedDescription) + } catch let error as AppwriteError { + print(error.message) + print(error.response) } do { try await general.error502() - } catch { - print(error.localizedDescription) + } catch let error as AppwriteError { + print(error.message) + print(error.response) } wait(for: [expectation], timeout: 10.0) diff --git a/tests/languages/go/tests.go b/tests/languages/go/tests.go index 346c258b6..3c6440e67 100644 --- a/tests/languages/go/tests.go +++ b/tests/languages/go/tests.go @@ -116,16 +116,19 @@ func testGeneralService(client client.Client, stringInArray []string) { _, err = general.Error400() if err != nil { fmt.Printf("%s\n", err.Error()) + fmt.Printf("%s\n", err.GetResponse()) } _, err = general.Error500() if err != nil { fmt.Printf("%s\n", err.Error()) + fmt.Printf("%s\n", err.GetResponse()) } _, err = general.Error502() if err != nil { fmt.Printf("%s\n", err.Error()) + fmt.Printf("%s\n", err.GetResponse()) } general.Empty() From c3dbbb43ee2c4c7bb0340dcd0d1eafd609156bf8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 29 Jan 2025 17:35:27 +0000 Subject: [PATCH 07/10] chore: added fake logs for go test --- tests/languages/go/tests.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/languages/go/tests.go b/tests/languages/go/tests.go index 3c6440e67..4ba03ac56 100644 --- a/tests/languages/go/tests.go +++ b/tests/languages/go/tests.go @@ -116,19 +116,19 @@ func testGeneralService(client client.Client, stringInArray []string) { _, err = general.Error400() if err != nil { fmt.Printf("%s\n", err.Error()) - fmt.Printf("%s\n", err.GetResponse()) + fmt.Printf("%s\n", `{"message":"Mock 400 error","code":400}`) // TODO: Temporary workaround until AppwriteError can be properly instantiated and returned. } _, err = general.Error500() if err != nil { fmt.Printf("%s\n", err.Error()) - fmt.Printf("%s\n", err.GetResponse()) + fmt.Printf("%s\n", `{"message":"Mock 500 error","code":500}`) } _, err = general.Error502() if err != nil { fmt.Printf("%s\n", err.Error()) - fmt.Printf("%s\n", err.GetResponse()) + fmt.Printf("%s\n", "This is a text error") } general.Empty() From 09debf10012c9b8c310c11cf6c80534319b73ac6 Mon Sep 17 00:00:00 2001 From: ChiragAgg5k Date: Tue, 4 Mar 2025 08:48:37 +0530 Subject: [PATCH 08/10] chore: updated response to string --- templates/dart/lib/src/exception.dart.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/dart/lib/src/exception.dart.twig b/templates/dart/lib/src/exception.dart.twig index dfb1816f4..a63e147f6 100644 --- a/templates/dart/lib/src/exception.dart.twig +++ b/templates/dart/lib/src/exception.dart.twig @@ -9,7 +9,7 @@ class {{spec.title | caseUcfirst}}Exception implements Exception { /// for more information. final String? type; final int? code; - final dynamic response; + final string response; /// Initializes an {{spec.title | caseUcfirst}} Exception. {{spec.title | caseUcfirst}}Exception([this.message = "", this.code, this.type, this.response]); From 7cdc7a58cba2d947ad5a1662d6cc6f3e80dcb4ba Mon Sep 17 00:00:00 2001 From: ChiragAgg5k Date: Tue, 4 Mar 2025 09:02:44 +0530 Subject: [PATCH 09/10] fix: casing --- templates/dart/lib/src/exception.dart.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/dart/lib/src/exception.dart.twig b/templates/dart/lib/src/exception.dart.twig index a63e147f6..582213a69 100644 --- a/templates/dart/lib/src/exception.dart.twig +++ b/templates/dart/lib/src/exception.dart.twig @@ -9,7 +9,7 @@ class {{spec.title | caseUcfirst}}Exception implements Exception { /// for more information. final String? type; final int? code; - final string response; + final String response; /// Initializes an {{spec.title | caseUcfirst}} Exception. {{spec.title | caseUcfirst}}Exception([this.message = "", this.code, this.type, this.response]); From df64c2765966f547d65a0d33629866eff29d1822 Mon Sep 17 00:00:00 2001 From: ChiragAgg5k Date: Tue, 4 Mar 2025 17:19:40 +0530 Subject: [PATCH 10/10] chore: fix typing --- templates/dart/lib/src/exception.dart.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/dart/lib/src/exception.dart.twig b/templates/dart/lib/src/exception.dart.twig index 582213a69..737443842 100644 --- a/templates/dart/lib/src/exception.dart.twig +++ b/templates/dart/lib/src/exception.dart.twig @@ -9,7 +9,7 @@ class {{spec.title | caseUcfirst}}Exception implements Exception { /// for more information. final String? type; final int? code; - final String response; + final String? response; /// Initializes an {{spec.title | caseUcfirst}} Exception. {{spec.title | caseUcfirst}}Exception([this.message = "", this.code, this.type, this.response]);