Skip to content

Commit d7672b6

Browse files
author
Kumanan Rajamanikkam
committed
Added support for error callbacks
1 parent 17f73e1 commit d7672b6

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

lib/swagger-spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,19 @@
239239
});
240240
describe("constructor", function() {
241241
beforeEach(function() {
242-
var body, callback, headers, operation;
242+
var body, callback, error, headers, operation;
243243
headers = {
244244
'mock': 'true'
245245
};
246246
body = null;
247247
callback = function() {
248248
return 'mock callback';
249249
};
250+
error = function() {
251+
return 'mock error';
252+
};
250253
operation = wordnik2.word.operations.getExamples;
251-
return window.request = new SwaggerRequest("GET", "http://google.com", headers, body, callback, operation);
254+
return window.request = new SwaggerRequest("GET", "http://google.com", headers, body, callback, error, operation);
252255
});
253256
return it("sticks the API key into the headers, if present in the parent Api configuration", function() {
254257
return runs(function() {

lib/swagger.js

Lines changed: 14 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/swagger-spec.coffee

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,10 @@ describe 'SwaggerRequest', ->
199199
body = null
200200
callback = ->
201201
'mock callback'
202+
error = ->
203+
'mock error'
202204
operation = wordnik2.word.operations.getExamples
203-
window.request = new SwaggerRequest("GET", "http://google.com", headers, body, callback, operation)
205+
window.request = new SwaggerRequest("GET", "http://google.com", headers, body, callback, error, operation)
204206

205207
it "sticks the API key into the headers, if present in the parent Api configuration", ->
206208
runs ->
@@ -235,4 +237,36 @@ describe 'SwaggerRequest', ->
235237
window.user?
236238

237239
runs ->
238-
expect(window.user.username).toBe("kareem#{random}")
240+
expect(window.user.username).toBe("kareem#{random}")
241+
242+
# it "supports PUT requests", ->
243+
# window.chorus = new SwaggerApi
244+
# discoveryUrl: "http://chorus-dev.nik.io/api/resources.json"
245+
# success: ->
246+
#
247+
# # Use a random suffix to pass uniqueness validations.
248+
# window.random = Math.floor(Math.random()*1000000)
249+
#
250+
# siteParts =
251+
# name: name
252+
# url : url
253+
# description : desc
254+
#
255+
# window.chorus.sites.createSite { body: siteParts }, (site) ->
256+
# updateSiteParts =
257+
# id: site.id
258+
# partnerId: site.partnerId
259+
# name: site.name
260+
# url : site.url
261+
# settings:
262+
# interBlogRecommendations: true
263+
# intraBlogRecommendations: false
264+
#
265+
# window.chorus.sites.updateSite {body: updateSiteParts}, (response) ->
266+
# window.site = response
267+
#
268+
# waitsFor ->
269+
# window.site?
270+
#
271+
# runs ->
272+
# expect(window.site.interBlogRecommendations).toBe(true)

src/swagger.coffee

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,19 @@ class SwaggerOperation
9595

9696
# Store a named reference to this operation on the parent resource
9797
# getDefinitions() maps to getDefinitionsData.do()
98-
@resource[@nickname]= (args, callback) =>
99-
@do(args, callback)
98+
@resource[@nickname]= (args, callback, error) =>
99+
@do(args, callback, error)
100100

101-
do: (args={}, callback = (data) -> console.log(data) ) =>
101+
do: (args={}, callback, error) =>
102+
103+
# Define a default error handler
104+
unless error?
105+
error = (xhr, textStatus, error) -> console.log xhr, textStatus, error
106+
107+
# Define a default success handler
108+
# TODO MAYBE: Call this success instead of callback
109+
unless callback?
110+
callback = (date) -> console.log data
102111

103112
# Pull headers out of args
104113
if args.headers?
@@ -110,7 +119,7 @@ class SwaggerOperation
110119
body = args.body
111120
delete args.body
112121

113-
new SwaggerRequest(@httpMethod, @urlify(args), headers, body, callback, this)
122+
new SwaggerRequest(@httpMethod, @urlify(args), headers, body, callback, error, this)
114123

115124
urlify: (args) ->
116125

@@ -142,10 +151,11 @@ class SwaggerOperation
142151

143152
class SwaggerRequest
144153

145-
constructor: (@type, @url, @headers, @body, @callback, @operation) ->
154+
constructor: (@type, @url, @headers, @body, @callback, @error, @operation) ->
146155
throw "SwaggerRequest type is required (get/post/put/delete)." unless @type?
147156
throw "SwaggerRequest url is required." unless @url?
148157
throw "SwaggerRequest callback is required." unless @callback?
158+
throw "SwaggerRequest error callback is required." unless @error?
149159
throw "SwaggerRequest operation is required." unless @operation?
150160

151161
# console.log "new SwaggerRequest: %o", this
@@ -165,7 +175,7 @@ class SwaggerRequest
165175
data: JSON.stringify(@body)
166176
dataType: 'json'
167177
error: (xhr, textStatus, error) ->
168-
console.log xhr, textStatus, error
178+
@error(xhr, textStatus, error)
169179
success: (data) =>
170180
@callback(data)
171181

0 commit comments

Comments
 (0)