Skip to content

Commit 1d0f316

Browse files
Revert "Add error treatment to apiUmbrellaWeb[...].createApiBackend - closes #357"
1 parent ebf0088 commit 1d0f316

File tree

2 files changed

+13
-90
lines changed

2 files changed

+13
-90
lines changed
Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,10 @@
11
AutoForm.hooks({
22
apiBackends: {
3-
beginSubmit: function () {
4-
// Disable form elements while submitting form
5-
$('[data-schema-key], button').attr("disabled", "disabled");
6-
},
7-
endSubmit: function () {
8-
// Enable form elements after form submission
9-
$('[data-schema-key], button').removeAttr("disabled");
10-
},
11-
before: {
12-
// Replace `formType` with the form `type` attribute to which this hook applies
13-
insert: function (apiBackendForm) {
14-
// Keep the context to use inside the callback function
15-
context = this;
16-
17-
// Send the API Backend to API Umbrella
18-
response = Meteor.call('createApiBackendOnApiUmbrella', apiBackendForm, function(error, apiUmbrellaWebResponse) {
19-
20-
//apiUmbrellaWebResponse contents
21-
// apiUmbrellaWebResponse = {
22-
// result: {},
23-
// http_status: 200,
24-
// errors: {}
25-
// };
26-
27-
if (apiUmbrellaWebResponse.http_status === 200) {
28-
//Return asynchronously
29-
context.result(apiBackendForm);
30-
} else {
31-
// ex 1:
32-
// {"default":'{"backend_protocol":["is not included in the list"]}}'
33-
// ex 2:
34-
// {"errors":{"frontend_host":["must be in the format of \"example.com\""],
35-
// "backend_host":["must be in the format of \"example.com\""],
36-
// "base":["must have at least one url_matches"],
37-
// "servers[0].host":["must be in the format of \"example.com\"","Could not resolve host: no address for http://api.example.com"],
38-
// "servers[0].port":["can't be blank","is not included in the list"]}
39-
// }
40-
41-
var errors = _.values(apiUmbrellaWebResponse.errors);
42-
errors = _.flatten(errors);
43-
_.each(errors, function(error) {
44-
console.log(error);
45-
//Display error to the user, keep the sAlert box visible.
46-
sAlert.error(error, {timeout: 'none'});
47-
// Figure out a way to send the errors back to the autoform fields, as if it were client validation.
48-
});
49-
50-
//Return error asynchronously
51-
context.result(false);
52-
}
53-
});
54-
}
55-
},
56-
onSuccess: function (formType, apiBackendId) {
57-
//Redirect to the just created API Backend page
58-
Router.go('viewApiBackend', {_id: apiBackendId});
3+
onSuccess: function (formType, backendId) {
4+
// Send the API Backend to API Umbrella
5+
Meteor.call('createApiBackendOnApiUmbrella', backendId);
6+
// Redirect to the just created API Backend page
7+
Router.go('viewApiBackend', {_id: backendId});
598
}
609
}
6110
});

server/methods/apiBackends.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Meteor.methods({
2-
syncApiBackends: function () {
2+
"syncApiBackends":function () {
33
// Check if API Umbrella settings are available
44
if (Meteor.settings.api_umbrella) {
55
// Get API Backends from API Umbrella instance
@@ -17,45 +17,19 @@ Meteor.methods({
1717
});
1818
};
1919
},
20-
createApiBackendOnApiUmbrella: function (apiBackendForm) {
21-
console.log('Submitting Backend to API Umbrella.');
20+
'createApiBackendOnApiUmbrella': function (apiBackendId) {
21+
console.log('Submitting Backend to API Umbrella.')
22+
// Get the API Backend object
23+
var apiBackend = ApiBackends.findOne(apiBackendId);
2224

2325
// Construct an API Backend object for API Umbrella with one 'api' key
2426
var constructedBackend = {
25-
"api": apiBackendForm
26-
};
27-
28-
var apiUmbrellaWebResponse = {
29-
result: {},
30-
http_status: 200,
31-
errors: {}
27+
"api": apiBackend
3228
};
3329

3430
// Send the API Backend to API Umbrella
35-
try {
36-
apiUmbrellaWebResponse.result = apiUmbrellaWeb.adminApi.v1.apiBackends.createApiBackend(constructedBackend);
37-
} catch (apiUmbrellaError) {
38-
39-
//apiUmbrellaError.message now is a string like
40-
// example 1:
41-
// '{"default":'{"backend_protocol":["is not included in the list"]}}'
42-
// ex 2:
43-
// '{"errors":{"frontend_host":["must be in the format of \"example.com\""],
44-
// "backend_host":["must be in the format of \"example.com\""],
45-
// "base":["must have at least one url_matches"],
46-
// "servers[0].host":["must be in the format of \"example.com\"","Could not resolve host: no address for http://api.example.com"],
47-
// "servers[0].port":["can't be blank","is not included in the list"]}'
48-
// }
49-
//after https://github.com/brylie/meteor-api-umbrella/issues/1 is closed, this code must be changed to something like:
50-
// apiUmbrellaWebResponse.errors = error.errors
51-
// apiUmbrellaWebResponse.status = error.http_status
52-
// or http://docs.meteor.com/#/full/meteor_error should be considered
53-
54-
//set the errors object
55-
apiUmbrellaWebResponse.errors = {'default': [apiUmbrellaError.message]};
56-
apiUmbrellaWebResponse.http_status = 422;
57-
}
31+
var response = apiUmbrellaWeb.adminApi.v1.apiBackends.createApiBackend(constructedBackend);
5832

59-
return apiUmbrellaWebResponse;
33+
// TODO: Add error checking to ensure backend successfully inserted in API Umbrella
6034
}
6135
});

0 commit comments

Comments
 (0)