From 7dfcd8bfe369815495fce74dbd3483036ba97f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 1 Jul 2022 09:36:25 +0200 Subject: [PATCH] MOBILE-4047 errors: Admit buttons when creating a CoreError --- src/core/classes/errors/errors.ts | 4 ++-- src/core/classes/errors/errorwithtitle.ts | 8 ++++++-- src/core/services/sites.ts | 4 ++-- src/core/services/utils/dom.ts | 7 ++++++- src/core/services/utils/text.ts | 4 +++- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/core/classes/errors/errors.ts b/src/core/classes/errors/errors.ts index 517d91910..9a4b21a23 100644 --- a/src/core/classes/errors/errors.ts +++ b/src/core/classes/errors/errors.ts @@ -23,7 +23,7 @@ import { CoreAjaxWSError } from './ajaxwserror'; import { CoreCaptureError } from './captureerror'; import { CoreNetworkError } from './network-error'; import { CoreSiteError } from './siteerror'; -import { CoreErrorWithTitle } from './errorwithtitle'; +import { CoreErrorWithOptions } from './errorwithtitle'; import { CoreHttpError } from './httperror'; export const CORE_ERRORS_CLASSES: Type[] = [ @@ -36,6 +36,6 @@ export const CORE_ERRORS_CLASSES: Type[] = [ CoreSilentError, CoreSiteError, CoreWSError, - CoreErrorWithTitle, + CoreErrorWithOptions, CoreHttpError, ]; diff --git a/src/core/classes/errors/errorwithtitle.ts b/src/core/classes/errors/errorwithtitle.ts index 0ff83d3b7..8a9e322b5 100644 --- a/src/core/classes/errors/errorwithtitle.ts +++ b/src/core/classes/errors/errorwithtitle.ts @@ -12,20 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { AlertButton } from '@ionic/angular'; import { CoreError } from './error'; /** * Error with an explicit title describing the problem (instead of just "Error" or a generic message). * This title should be used to communicate the problem with users, and if it's undefined it should be omitted. + * The error also may contain customizable action buttons. */ -export class CoreErrorWithTitle extends CoreError { +export class CoreErrorWithOptions extends CoreError { title?: string; + buttons?: AlertButton[]; - constructor(message?: string, title?: string) { + constructor(message?: string, title?: string, buttons?: AlertButton[]) { super(message); this.title = title; + this.buttons = buttons; } } diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index d508b06b3..3307ce10d 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -51,7 +51,7 @@ import { CoreRedirectPayload } from './navigator'; import { CoreSitesFactory } from './sites-factory'; import { CoreText } from '@singletons/text'; import { CoreLoginHelper } from '@features/login/services/login-helper'; -import { CoreErrorWithTitle } from '@classes/errors/errorwithtitle'; +import { CoreErrorWithOptions } from '@classes/errors/errorwithtitle'; import { CoreAjaxError } from '@classes/errors/ajaxerror'; import { CoreAjaxWSError } from '@classes/errors/ajaxwserror'; import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; @@ -870,7 +870,7 @@ export class CoreSitesProvider { const siteUrlAllowed = await CoreLoginHelper.isSiteUrlAllowed(site.getURL(), false); if (!siteUrlAllowed) { - throw new CoreErrorWithTitle(Translate.instant('core.login.sitenotallowed')); + throw new CoreErrorWithOptions(Translate.instant('core.login.sitenotallowed')); } this.currentSite = site; diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index a6218fd05..3ee12557a 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -1344,7 +1344,6 @@ export class CoreDomUtilsProvider { const alertOptions: AlertOptions = { message: message, - buttons: [Translate.instant('core.ok')], }; if (this.isNetworkError(message, error)) { @@ -1355,6 +1354,12 @@ export class CoreDomUtilsProvider { alertOptions.header = Translate.instant('core.error'); } + if (typeof error !== 'string' && 'buttons' in error && typeof error.buttons !== 'undefined') { + alertOptions.buttons = error.buttons; + } else { + alertOptions.buttons = [Translate.instant('core.ok')]; + } + return this.showAlertWithOptions(alertOptions, autocloseTime); } diff --git a/src/core/services/utils/text.ts b/src/core/services/utils/text.ts index 20d98c515..6c024c41a 100644 --- a/src/core/services/utils/text.ts +++ b/src/core/services/utils/text.ts @@ -26,6 +26,7 @@ import { CoreFileHelper } from '@services/file-helper'; import { CoreDomUtils } from './dom'; import { CoreText } from '@singletons/text'; import { CoreUrl } from '@singletons/url'; +import { AlertButton } from '@ionic/angular'; /** * Different type of errors the app can treat. @@ -38,6 +39,7 @@ export type CoreTextErrorObject = { debuginfo?: string; backtrace?: string; title?: string; + buttons?: AlertButton[]; }; /* @@ -166,7 +168,7 @@ export class CoreTextUtilsProvider { improvedError = error; } - improvedError.title = title; + improvedError.title = improvedError.title || title; return improvedError; }