Skip to content

Commit

Permalink
chore: fixed response types across sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiragAgg5k committed Jan 29, 2025
1 parent 2d02e13 commit 1ec4519
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 21 deletions.
3 changes: 2 additions & 1 deletion templates/apple/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ open class Client {
throw {{ spec.title | caseUcfirst }}Error(
message: message,
code: Int(response.status.code),
type: type
type: type,
data
)
}
}
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
2 changes: 1 addition & 1 deletion templates/dart/lib/src/client_mixin.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ClientMixin {
response['message'],
response['code'],
response['type'],
response,
res.body,
);
} else {
throw {{spec.title | caseUcfirst}}Exception(res.body);
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
16 changes: 11 additions & 5 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,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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion templates/flutter/lib/src/client_mixin.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ClientMixin {
response['message'],
response['code'],
response['type'],
response,
res.body,
);
} else {
throw {{spec.title | caseUcfirst}}Exception(res.body);
Expand Down
5 changes: 3 additions & 2 deletions templates/node/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion templates/python/package/client.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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
2 changes: 1 addition & 1 deletion templates/ruby/lib/container/client.rb.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions templates/swift/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ open class Client {
throw {{ spec.title | caseUcfirst }}Error(
message: message,
code: Int(response.status.code),
type: type
type: type,
data
)
}

Expand Down Expand Up @@ -382,7 +383,8 @@ open class Client {
throw {{ spec.title | caseUcfirst }}Error(
message: message,
code: Int(response.status.code),
type: type
type: type,
data
)
}
}
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? = nil) {
self.message = message
self.code = code
self.type = type
self.response = response
}
}

Expand Down
5 changes: 3 additions & 2 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand Down

0 comments on commit 1ec4519

Please sign in to comment.