MOBILE-3565 errors: Create several error classes

main
Dani Palou 2020-10-14 08:27:02 +02:00
parent 9cc98e267e
commit 2dbd1a6d98
10 changed files with 217 additions and 3 deletions

View File

@ -0,0 +1,30 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreError } from '@classes/errors/error';
/**
* Generic error returned by an Ajax call.
*/
export class CoreAjaxError extends CoreError {
available?: number; // Whether the AJAX call is available. 0 if unknown, 1 if available, -1 if not available.
constructor(message: string, available?: number) {
super(message);
this.available = typeof available == 'undefined' ? 0 : available;
}
}

View File

@ -0,0 +1,53 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreError } from '@classes/errors/error';
/**
* Error returned by WS.
*/
export class CoreAjaxWSError extends CoreError {
exception?: string; // Name of the Moodle exception.
errorcode?: string;
warningcode?: string;
link?: string; // Link to the site.
moreinfourl?: string; // Link to a page with more info.
debuginfo?: string; // Debug info. Only if debug mode is enabled.
backtrace?: string; // Backtrace. Only if debug mode is enabled.
available?: number; // Whether the AJAX call is available. 0 if unknown, 1 if available, -1 if not available.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
constructor(error: any, available?: number) {
super(error.message);
this.exception = error.exception;
this.errorcode = error.errorcode;
this.warningcode = error.warningcode;
this.link = error.link;
this.moreinfourl = error.moreinfourl;
this.debuginfo = error.debuginfo;
this.backtrace = error.backtrace;
this.available = available;
if (typeof this.available == 'undefined') {
if (this.errorcode) {
this.available = this.errorcode == 'invalidrecord' ? -1 : 1;
} else {
this.available = 0;
}
}
}
}

View File

@ -0,0 +1,20 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreSilentError } from '@classes/errors/silenterror';
/**
* User canceled an action.
*/
export class CoreCanceledError extends CoreSilentError { }

View File

@ -0,0 +1,20 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreError } from '@classes/errors/error';
/**
* Error that won't be displayed to the user.
*/
export class CoreSilentError extends CoreError { }

View File

@ -0,0 +1,41 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreError } from '@classes/errors/error';
/**
* Error returned when performing operations regarding a site (check if it exists, authenticate user, etc.).
*/
export class CoreSiteError extends CoreError {
errorcode?: string;
critical?: boolean;
loggedOut?: boolean;
constructor(protected error: SiteError) {
super(error.message);
this.errorcode = error.errorcode;
this.critical = error.critical;
this.loggedOut = error.loggedOut;
}
}
export type SiteError = {
message: string;
errorcode?: string;
critical?: boolean; // Whether the error is important enough to abort the operation.
loggedOut?: boolean; // Whether site has been marked as logged out.
};

View File

@ -0,0 +1,42 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CoreError } from '@classes/errors/error';
/**
* Error returned by WS.
*/
export class CoreWSError extends CoreError {
exception?: string; // Name of the Moodle exception.
errorcode?: string;
warningcode?: string;
link?: string; // Link to the site.
moreinfourl?: string; // Link to a page with more info.
debuginfo?: string; // Debug info. Only if debug mode is enabled.
backtrace?: string; // Backtrace. Only if debug mode is enabled.
constructor(error: any) {
super(error.message);
this.exception = error.exception;
this.errorcode = error.errorcode;
this.warningcode = error.warningcode;
this.link = error.link;
this.moreinfourl = error.moreinfourl;
this.debuginfo = error.debuginfo;
this.backtrace = error.backtrace;
}
}

View File

@ -16,7 +16,7 @@ import { Injectable } from '@angular/core';
import { Coordinates } from '@ionic-native/geolocation';
import { CoreApp } from '@services/app';
import { CoreError } from '@classes/error';
import { CoreError } from '@classes/errors/error';
import { Geolocation, Diagnostic, makeSingleton } from '@singletons/core.singletons';
@Injectable()

View File

@ -17,6 +17,7 @@ import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { CoreApp } from '@services/app';
import { CoreLang } from '@services/lang';
import { CoreError } from '@classes/errors/error';
import { makeSingleton, Translate } from '@singletons/core.singletons';
import { CoreWSExternalFile } from '@services/ws';
import { Locutus } from '@singletons/locutus';
@ -29,6 +30,8 @@ export type CoreTextErrorObject = {
error?: string;
content?: string;
body?: string;
debuginfo?: string;
backtrace?: string;
};
/*
@ -526,10 +529,13 @@ export class CoreTextUtilsProvider {
* @param error Error object.
* @return Error message, undefined if not found.
*/
getErrorMessageFromError(error: string | CoreTextErrorObject): string {
getErrorMessageFromError(error: string | CoreError | CoreTextErrorObject): string {
if (typeof error == 'string') {
return error;
}
if (error instanceof CoreError) {
return error.message;
}
return error && (error.message || error.error || error.content || error.body);
}

View File

@ -21,10 +21,11 @@ import { CoreApp } from '@services/app';
import { CoreEvents, CoreEventsProvider } from '@services/events';
import { CoreFile } from '@services/file';
import { CoreLang } from '@services/lang';
import { CoreWS, CoreWSError, CoreWSExternalFile } from '@services/ws';
import { CoreWS, CoreWSExternalFile } from '@services/ws';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreMimetypeUtils } from '@services/utils/mimetype';
import { CoreTextUtils } from '@services/utils/text';
import { CoreWSError } from '@classes/errors/wserror';
import {
makeSingleton, Clipboard, InAppBrowser, Platform, FileOpener, WebIntent, QRScanner, Translate,
} from '@singletons/core.singletons';
@ -325,6 +326,7 @@ export class CoreUtilsProvider {
* @param message The message to include in the error.
* @param needsTranslate If the message needs to be translated.
* @return Fake WS error.
* @deprecated since 3.9.5. Just create the error directly.
*/
createFakeWSError(message: string, needsTranslate?: boolean): CoreWSError {
return CoreWS.instance.createFakeWSError(message, needsTranslate);