ErrorUtils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. *
  5. * "Restricted Materials of IBM"
  6. *
  7. * 5746-SM2
  8. *
  9. * (C) Copyright IBM Corp. 2015, 2020
  10. *
  11. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  12. */
  13. define(['underscore'], function (_) {
  14. return {
  15. _getResponse: function _getResponse(request) {
  16. var response = request.responseJSON;
  17. if (!response) {
  18. if (request.responseText && request.responseText.length > 0) {
  19. response = JSON.parse(request.responseText); //TODO: JSON.parse generally should have try/catch
  20. }
  21. }
  22. return response || {};
  23. },
  24. _getCAFCode: function _getCAFCode(response) {
  25. var code;
  26. // For some reason, caf blocks the real code. We check the code in the message itself
  27. var m = response.messages && response.messages[0];
  28. if (m && m.indexOf('CM-REQ-4032') !== -1) {
  29. code = 'cmEmptySelection';
  30. }
  31. return code;
  32. },
  33. showError: function showError(glassContext, request, options) {
  34. var response = this._getResponse(request);
  35. var message = void 0;
  36. var code = response.errorCode;
  37. var stringResource = glassContext.appController.currentAppView.currentContentView.services.getSvcSync('.StringResources');
  38. // Check if we have a nicer message in our resources.
  39. if (code) {
  40. if (code === 'caf') {
  41. // For some reason, caf blocks the real code. We check the code in the message itself
  42. code = this._getCAFCode(response) || code;
  43. }
  44. message = stringResource.get(code, _.extend({
  45. _: 'NOT_FOUND'
  46. }, options));
  47. }
  48. if (!message || message === 'NOT_FOUND') {
  49. message = response.messages && response.messages[0];
  50. }
  51. if (!message || message.length === 0) {
  52. message = stringResource.get('saveFailed');
  53. }
  54. // In case we show a dialog after the toast (e.g. saveas dialog) we want the toast to appear on top
  55. setTimeout(function () {
  56. glassContext.appController.showToast(message, {
  57. type: 'error',
  58. preventDuplicates: true
  59. });
  60. }, 10);
  61. return code;
  62. },
  63. getErrorCode: function getErrorCode(request) {
  64. var response = request.responseJSON;
  65. if (!response) {
  66. response = JSON.parse(request.responseText); //TODO: This could call getResponse
  67. }
  68. return response.errorCode;
  69. },
  70. /**
  71. * check is the current session user has a capability
  72. * it relies on the fact that .DashboardCoreService is registered
  73. * @param {object} api - glassContext or dashboardAPI
  74. * @param {boolean} capability
  75. */
  76. hasCapability: function hasCapability(api) {
  77. var capability = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'canUseInteractiveDashboard';
  78. var userProfileService = void 0;
  79. if (api.getCoreSvc) {
  80. var dashboardCoreService = api.getCoreSvc('.DashboardCoreService');
  81. userProfileService = dashboardCoreService.getFeature('UserProfile');
  82. } else {
  83. userProfileService = api.getFeature('UserProfile');
  84. }
  85. return userProfileService.hasCapability(capability);
  86. }
  87. };
  88. });
  89. //# sourceMappingURL=ErrorUtils.js.map