Skip to content

Commit aeb3009

Browse files
authored
Merge pull request #125 from lohanidamodar/feat-ruby-exception
Feat Ruby SDK exception
2 parents 12a6a4b + 0b7443f commit aeb3009

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

src/SDK/Language/Ruby.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ public function getFiles()
136136
'template' => '/ruby/lib/container/file.rb.twig',
137137
'minify' => false,
138138
],
139+
[
140+
'scope' => 'default',
141+
'destination' => 'lib/{{ spec.title | caseDash }}/exception.rb',
142+
'template' => '/ruby/lib/container/exception.rb.twig',
143+
'minify' => false,
144+
],
139145
[
140146
'scope' => 'service',
141147
'destination' => '/lib/{{ spec.title | caseDash}}/services/{{service.name | caseDash}}.rb',

templates/ruby/lib/container.rb.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require 'json'
44
require 'mime/types'
55
require_relative '{{ spec.title | caseDash }}/client'
66
require_relative '{{ spec.title | caseDash }}/service'
7+
require_relative '{{ spec.title | caseDash }}/exception'
78
require_relative '{{ spec.title | caseDash }}/file'
89
{% for service in spec.services %}
910
require_relative '{{ spec.title | caseDash }}/services/{{ service.name | caseDash }}'

templates/ruby/lib/container/client.rb.twig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module {{spec.title | caseUcfirst}}
8383
begin
8484
response = http.send_request(method.upcase, uri.request_uri, payload, headers)
8585
rescue => error
86-
raise 'Request Failed: ' + error.message
86+
raise {{spec.title | caseUcfirst}}::Exception.new(error.message)
8787
end
8888

8989
# Handle Redirects
@@ -94,7 +94,13 @@ module {{spec.title | caseUcfirst}}
9494
return fetch(method, uri, headers, {}, limit - 1)
9595
end
9696

97-
return JSON.parse(response.body);
97+
res = JSON.parse(response.body);
98+
99+
if(response.code.to_i >= 400)
100+
raise {{spec.title | caseUcfirst}}::Exception.new(res['message'], res['status'], res)
101+
end
102+
103+
return res;
98104
end
99105

100106
def encodeFormData(value, key=nil)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Appwrite
2+
class Exception < StandardError
3+
@code = 0;
4+
@response = nil;
5+
6+
def initialize(message, code, response)
7+
super(message)
8+
@code = code
9+
@response = response
10+
end
11+
attr_reader :code
12+
attr_reader :response
13+
end
14+
end

tests/languages/ruby/tests.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,16 @@
4949

5050
file = Appwrite::File.new('./tests/resources/file.png')
5151
response = general.upload(x:'string',y: 123,z:['string in array'], file: file);
52-
puts response['result']
52+
puts response['result']
53+
54+
begin
55+
response = general.error400()
56+
rescue Appwrite::Exception => error
57+
puts error.message
58+
end
59+
60+
begin
61+
response = general.error500()
62+
rescue Appwrite::Exception => error
63+
puts error.message
64+
end

0 commit comments

Comments
 (0)