Skip to content

fix: replace thrown errors with logs #1220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased](https://github.com/Instabug/Instabug-React-Native/compare/v13.4.0...dev)

### Fixed

- Replace thrown errors with logs ([#1220](https://github.com/Instabug/Instabug-React-Native/pull/1220))

## [13.4.0](https://github.com/Instabug/Instabug-React-Native/compare/v13.3.0...v13.4.0) (October 2, 2024)

### Added
Expand Down
8 changes: 6 additions & 2 deletions src/modules/Instabug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as NetworkLogger from './NetworkLogger';
import { captureUnhandledRejections } from '../utils/UnhandledRejectionTracking';
import type { ReproConfig } from '../models/ReproConfig';
import type { FeatureFlag } from '../models/FeatureFlag';
import InstabugConstants from '../utils/InstabugConstants';

let _currentScreen: string | null = null;
let _lastScreen: string | null = null;
Expand Down Expand Up @@ -381,7 +382,8 @@ export const setReproStepsConfig = (config: ReproConfig) => {
*/
export const setUserAttribute = (key: string, value: string) => {
if (!key || typeof key !== 'string' || typeof value !== 'string') {
throw new TypeError('Invalid param, Expected String');
console.error(InstabugConstants.SET_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE);
return;
}
NativeInstabug.setUserAttribute(key, value);
};
Expand All @@ -404,7 +406,9 @@ export const getUserAttribute = async (key: string): Promise<string | null> => {
*/
export const removeUserAttribute = (key: string) => {
if (!key || typeof key !== 'string') {
throw new TypeError('Invalid param, Expected String');
console.error(InstabugConstants.REMOVE_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE);

return;
}
NativeInstabug.removeUserAttribute(key);
};
Expand Down
4 changes: 4 additions & 0 deletions src/utils/InstabugConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const InstabugConstants = {
'The response body has not been logged because it exceeds the maximum size of 10 Kb',
MAX_REQUEST_BODY_SIZE_EXCEEDED_MESSAGE:
'The request body has not been logged because it exceeds the maximum size of 10 Kb',
SET_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE:
'IBG-RN: Expected key and value passed to setUserAttribute to be of type string',
REMOVE_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE:
'IBG-RN: Expected key and value passed to removeUserAttribute to be of type string',
};

export default InstabugConstants;
1 change: 0 additions & 1 deletion test/modules/APM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe('APM Module', () => {

it("should throw an error if native startExecutionTrace didn't return an ID", async () => {
mocked(NativeAPM).startExecutionTrace.mockResolvedValueOnce(null);

const promise = APM.startExecutionTrace('trace');

await expect(promise).rejects.toThrowError(/trace "trace" wasn't created/i);
Expand Down
27 changes: 17 additions & 10 deletions test/modules/Instabug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../../src/utils/Enums';
import InstabugUtils from '../../src/utils/InstabugUtils';
import type { FeatureFlag } from '../../src/models/FeatureFlag';
import InstabugConstants from '../../src/utils/InstabugConstants';

describe('Instabug Module', () => {
beforeEach(() => {
Expand Down Expand Up @@ -640,10 +641,13 @@ describe('Instabug Module', () => {
[{}, 'value'],
['key', []],
])("should fail if key and value aren't strings when calling setUserAttribute", (key, value) => {
// @ts-ignore
expect(() => Instabug.setUserAttribute(key, value)).toThrow(TypeError);
const logSpy = jest.spyOn(console, 'error');

// @ts-ignore
Instabug.setUserAttribute(key, value);
expect(NativeInstabug.setUserAttribute).not.toBeCalled();
expect(logSpy).toHaveBeenCalledWith(InstabugConstants.SET_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE);
logSpy.mockRestore();
});

it('should call the native method setUserAttribute', () => {
Expand Down Expand Up @@ -676,14 +680,17 @@ describe('Instabug Module', () => {
expect(NativeInstabug.removeUserAttribute).toBeCalledWith(key);
});

it.each([null, 1, {}])(
"should fail if key isn't a string when calling removeUserAttribute",
(key) => {
// @ts-ignore
expect(() => Instabug.removeUserAttribute(key)).toThrow(TypeError);
expect(NativeInstabug.removeUserAttribute).not.toBeCalled();
},
);
it.each([[null]])("should fail if key isn't a string when calling removeUserAttribute", (key) => {
const logSpy = jest.spyOn(console, 'error');

// @ts-ignore
Instabug.removeUserAttribute(key);
expect(NativeInstabug.removeUserAttribute).not.toBeCalled();
expect(logSpy).toHaveBeenCalledWith(
InstabugConstants.REMOVE_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE,
);
logSpy.mockRestore();
});

it('should call native method getAllUserAttributes', async () => {
const expected = { type: 'guest' };
Expand Down