Skip to content

Commit

Permalink
Merge pull request #1031 from appwrite/fix-execption-response
Browse files Browse the repository at this point in the history
chore: fix response attribute from AppwriteException
  • Loading branch information
christyjacob4 authored Mar 6, 2025
2 parents ea2b16f + df64c27 commit 56f0948
Show file tree
Hide file tree
Showing 34 changed files with 134 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion templates/apple/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,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
type: type,
response: responseString
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/cli/lib/client.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
4 changes: 2 additions & 2 deletions templates/dart/lib/src/client_mixin.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ mixin ClientMixin {
response['message'],
response['code'],
response['type'],
response,
res.body,
);
} else {
throw {{spec.title | caseUcfirst}}Exception(res.body);
throw {{spec.title | caseUcfirst}}Exception(res.body, res.statusCode, '', res.body);
}
}
dynamic data;
Expand Down
2 changes: 1 addition & 1 deletion templates/dart/lib/src/exception.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion templates/deno/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
22 changes: 16 additions & 6 deletions templates/dotnet/Package/Client.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -239,10 +241,13 @@ 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() ?? string.Empty;
} else {
message = text;
}

throw new {{spec.title | caseUcfirst}}Exception(message, code);
throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text);
}

return response.Headers.Location.OriginalString;
Expand Down Expand Up @@ -286,13 +291,18 @@ 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();
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);
throw new {{spec.title | caseUcfirst}}Exception(message, code, type, text);
}

if (isJson)
Expand Down
4 changes: 2 additions & 2 deletions templates/flutter/lib/src/client_mixin.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ mixin ClientMixin {
response['message'],
response['code'],
response['type'],
response,
res.body,
);
} else {
throw {{spec.title | caseUcfirst}}Exception(res.body);
throw {{spec.title | caseUcfirst}}Exception(res.body, res.statusCode, '', res.body);
}
}
dynamic data;
Expand Down
7 changes: 7 additions & 0 deletions templates/go/client.go.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
type {{ spec.title | caseUcfirst }}Error struct {
statusCode int
message string
response string
}

// ClientResponse - represents the client response
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions templates/node/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class Client {
const { uri, options } = this.prepareRequest(method, url, headers, params);

let data: any = null;
let text: string = '';

const response = await fetch(uri, options);

Expand All @@ -273,16 +274,18 @@ 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: 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;
Expand Down
2 changes: 1 addition & 1 deletion templates/php/src/Client.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions templates/python/package/client.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ 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)
raise {{spec.title | caseUcfirst}}Exception(response.text, response.status_code, None, response.text)
else:
raise {{spec.title | caseUcfirst}}Exception(e)

Expand Down
5 changes: 3 additions & 2 deletions templates/react-native/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions templates/ruby/lib/container/client.rb.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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'], result)
raise {{spec.title | caseUcfirst}}::Exception.new(result['message'], result['status'], result['type'], response.body)
end

unless response_type.respond_to?("from")
Expand All @@ -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?")
Expand Down
12 changes: 10 additions & 2 deletions templates/swift/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +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
type: type,
response: responseString
)
}

Expand Down Expand Up @@ -369,20 +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
type: type,
response: responseString
)
}
}
Expand Down
4 changes: 3 additions & 1 deletion templates/swift/Sources/Models/Error.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "") {
self.message = message
self.code = code
self.type = type
self.response = response
}
}

Expand Down
7 changes: 5 additions & 2 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ class Client {
const { uri, options } = this.prepareRequest(method, url, headers, params);

let data: any = null;
let text: string = '';

const response = await fetch(uri, options);

Expand All @@ -679,16 +680,18 @@ 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: 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');
Expand Down
7 changes: 7 additions & 0 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand Down
Loading

0 comments on commit 56f0948

Please sign in to comment.