"Custom Ajax calls" from a coach means calling the server without leaving the page. In BPM / BAW you have these methods, each with its own error handling:
- Ajax service of a coach view (a service flow bound to a configuration option of type Service) - the built-in mechanism; the view calls it with this.context.options.myService(callbacks):
// custom coach view: option "lookupService" (Service) - input/output objects
var _this = this;
this.context.options.lookupService({
params: { customerId: id },
load: function (data) { _this.context.binding.set("value", data.customer); },
error: function (err) { _this.context.element.querySelector(".msg").textContent = "Lookup failed: " + (err.errorMessage || err); }
});- UI Toolkit Service Call / Data controls - no code for the call; the On error event receives {errorCode, errorText, serviceInError}, On result the output; a busy indicator is built in.
- Direct REST calls with XMLHttpRequest / fetch to the BPM REST API (task, process, search) or to any API reachable from the browser: handle HTTP status codes yourself (401/403 = session or role, 500 = BPM error with Data.errorMessage), send BPMCSRFToken on BAW 20+ / CP4BA, and mind CORS for foreign hosts.
- Service flow called from the CSHS diagram (not strictly Ajax but the same effect with Stay on page boundary events): errors are error events on the step (question 2887).
Error handling that works in every method: fail fast on the server (service flow ends in an Error end event with a typed error object), map to a user message in one place (a coach view or a client-side script that reads tw.local.err), never swallow silently (write to the console and to an audit service), and time out (Service Call control option / xhr.timeout) so that a hanging back end does not freeze the coach.
References