Skip to content

Commit fdefa4e

Browse files
Merge pull request #565 from apinf/feature/api-backend-error-handling
Feature/api backend error handling - redoes #527
2 parents 39232ef + e618c4e commit fdefa4e

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed
Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,61 @@
11
AutoForm.hooks({
22
apiBackends: {
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});
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+
insert: function (apiBackendForm) {
13+
// Keep the context to use inside the callback function
14+
var context = this;
15+
16+
// Send the API Backend to API Umbrella
17+
response = Meteor.call('createApiBackendOnApiUmbrella', apiBackendForm, function(error, apiUmbrellaWebResponse) {
18+
19+
//apiUmbrellaWebResponse contents
20+
// apiUmbrellaWebResponse = {
21+
// result: {},
22+
// http_status: 200,
23+
// errors: {}
24+
// };
25+
26+
if (apiUmbrellaWebResponse.http_status === 200) {
27+
// Submit form on meteor:api-umbrella success
28+
context.result(apiBackendForm);
29+
} else {
30+
// Error data structure returned.
31+
// nowadays, jerry-rig solution:
32+
// {"default":'{"backend_protocol":["is not included in the list"]}}'
33+
// after https://github.com/brylie/meteor-api-umbrella/issues/1 is resolved, it should be:
34+
// {"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+
var errors = _.values(apiUmbrellaWebResponse.errors);
40+
41+
// Flatten all error descriptions to show using sAlert
42+
errors = _.flatten(errors);
43+
_.each(errors, function(error) {
44+
//Display error to the user, keep the sAlert box visible.
45+
sAlert.error(error, {timeout: 'none'});
46+
// TODO: Figure out a way to send the errors back to the autoform fields, as if it were client validation,
47+
// and get rid of sAlert here.
48+
});
49+
50+
//Cancel form submission on error, so user see the sAlert.error message and edit the incorrect fields
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});
859
}
960
}
1061
});

server/methods/apiBackends.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,42 @@ Meteor.methods({
1717
});
1818
};
1919
},
20-
'createApiBackendOnApiUmbrella': function (apiBackendId) {
21-
console.log('Submitting Backend to API Umbrella.')
22-
// Get the API Backend object
23-
var apiBackend = ApiBackends.findOne(apiBackendId);
24-
20+
createApiBackendOnApiUmbrella: function (apiBackendForm) {
2521
// Construct an API Backend object for API Umbrella with one 'api' key
2622
var constructedBackend = {
27-
"api": apiBackend
23+
"api": apiBackendForm
24+
};
25+
26+
// Response object to be send back to client layer.
27+
var apiUmbrellaWebResponse = {
28+
result: {},
29+
http_status: 200,
30+
errors: {}
2831
};
2932

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

33-
// TODO: Add error checking to ensure backend successfully inserted in API Umbrella
53+
//set the errors object
54+
apiUmbrellaWebResponse.errors = {'default': [apiUmbrellaError.message]};
55+
apiUmbrellaWebResponse.http_status = 422;
56+
}
3457
}
3558
});

0 commit comments

Comments
 (0)