react-native-malwarelytics
Version:
Malwarelytics for React Native protects your banking or fintech app from a broad range of mobile security threats with an industry-leading mobile threat intelligence solution.
106 lines (101 loc) • 3.98 kB
text/typescript
//
// Copyright 2023 Wultra s.r.o.
//
// 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.
//
/**
* Error object reported in case of the error.
*/
export class MalwarelyticsError {
/**
* Error code
*/
readonly code: MalwarelyticsErrorCode
/**
* Error message
*/
readonly message: string | undefined
/**
* Original exception, if this is a "GENERIC_ERROR"
*/
readonly originalException: any
/**
* Wrap any error into MalwarelyticsError object.
* @param error Error object to wrap.
* @returns `MalwarelyticsError` object created from given error.
*/
static wrap(error: any): MalwarelyticsError {
if (error instanceof MalwarelyticsError) {
return error
}
const code = isKnownErrorCode(error.code)
//console.log(`${Platform.OS}: Going to wrap error: ${JSON.stringify(error)}`)
if (code != undefined) {
//console.log(`${Platform.OS}: Wrapping error with code ${code}`)
return new MalwarelyticsError(error.code, error.message, error)
}
if (error instanceof Error) {
//console.log(`${Platform.OS}: Wrapping as Error`)
return new MalwarelyticsError("GENERIC_ERROR", error.message, error)
}
if (error.message != undefined) {
//console.log(`${Platform.OS}: Wrapping as generic error with message`)
return new MalwarelyticsError("GENERIC_ERROR", error.message, error)
}
//console.log(`${Platform.OS}: Wrapping fallback`)
return new MalwarelyticsError("GENERIC_ERROR", "Unknown error", error)
}
/**
* Construct error object.
* @param code Error
* @param message
* @param exception
*/
constructor(code: MalwarelyticsErrorCode, message: string | undefined = undefined, exception: any = undefined) {
this.code = code
this.message = message
this.originalException = exception
}
}
/**
* Error codes:
* - `"LINKING_ERROR"` - Library is not properly linked with the native code.
* - `"GENERIC_ERROR"` - Generic error. See `originalException` for more details.
* - `"WRONG_STATE"` - Method called in the wrong object's state.
* - `"INVALID_CONFIG"` - Invalid configuration object.
* - `"MISSING_CONFIG"` - Missing required object in configuration.
* - `"INVALID_PARAM"` - Method called with an invalid parameter.
* - `"MISSING_PARAM"` - Method called with a missing required parameter.
* - `"METHOD_NOT_SUPPORTED"` - Method is not supported on this platform.
* - `"METHOD_NOT_AVAILABLE"` - Method is not available in the current configuration.
*/
export type MalwarelyticsErrorCode = "LINKING_ERROR" | "GENERIC_ERROR" |
"WRONG_STATE" |
"INVALID_CONFIG" | "MISSING_CONFIG" |
"INVALID_PARAM" | "MISSING_PARAM" |
"METHOD_NOT_SUPPORTED" | "METHOD_NOT_AVAILABLE";
const allErrorCodes: MalwarelyticsErrorCode[] = [
'LINKING_ERROR', 'GENERIC_ERROR',
'WRONG_STATE',
'INVALID_CONFIG', 'MISSING_CONFIG',
'INVALID_PARAM', 'MISSING_PARAM',
'METHOD_NOT_SUPPORTED', 'METHOD_NOT_AVAILABLE'
]
/**
* Determine whether the provided string is error code specified in the library.
* @param code Code to evaluate.
* @returns Typed error code or undefined if code is not produced in this library.
*/
function isKnownErrorCode(code: string): MalwarelyticsErrorCode | undefined {
return allErrorCodes.find(item => item === code)
}