⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default class Animation {
);

return true;
} catch (e) {
} catch (e: unknown) {
throw e;
} finally {
NativeAnimatedHelper.API.unsetWaitingForIdentifier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class MessageQueue {
} else {
try {
fn();
} catch (error) {
} catch (error: unknown) {
ErrorUtils.reportFatalError(error);
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/react-native/Libraries/Core/Timers/JSTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import NativeTiming from './NativeTiming';

const toError = require('../../../src/private/utilities/toError').default;
const BatchedBridge = require('../../BatchedBridge/BatchedBridge').default;
const Systrace = require('../../Performance/Systrace');
const invariant = require('invariant');
Expand Down Expand Up @@ -129,9 +130,9 @@ function _callTimer(timerID: number, frameTime: number, didTimeout: ?boolean) {
} else {
console.error('Tried to call a callback with invalid type: ' + type);
}
} catch (e) {
} catch (e: unknown) {
// Don't rethrow so that we can run all timers.
errors.push(e);
errors.push(toError(e));
}

if (__DEV__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ const NativeTiming = {
setSendIdleEvents: jest.fn(),
};

jest
.enableAutomock()
.mock('../NativeTiming', () => ({
__esModule: true,
default: NativeTiming,
}))
.unmock('../JSTimers');
jest.mock('../NativeTiming', () => ({
__esModule: true,
default: NativeTiming,
}));

const JSTimers = require('../JSTimers').default;

Expand Down
6 changes: 4 additions & 2 deletions packages/react-native/Libraries/Core/setUpErrorHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ if (global.RN$useAlwaysAvailableJSErrorHandling !== true) {
* You can use this module directly, or just require InitializeCore.
*/
const ExceptionsManager = require('./ExceptionsManager').default;
const toError = require('../../src/private/utilities/toError').default;
ExceptionsManager.installConsoleErrorReporter();

// Set up error handler
if (!global.__fbDisableExceptionsManager) {
const handleError = (e: unknown, isFatal: boolean) => {
try {
ExceptionsManager.handleException(e, isFatal);
} catch (ee) {
console.log('Failed to print error: ', ee.message);
} catch (ee: unknown) {
const error = toError(ee);
console.log('Failed to print error: ', error.message);
throw e;
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Core/setUpReactDevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if (__DEV__) {
try {
const parsedSettings = JSON.parse(serializedHookSettings);
hookSettings = parsedSettings;
} catch {
} catch (e: unknown) {
console.error(
'Failed to parse persisted React DevTools hook settings. React DevTools will be initialized with default settings.',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import type {EventSubscription} from '../vendor/emitter/EventEmitter';

const toError = require('../../src/private/utilities/toError').default;
const invariant = require('invariant');

export type SimpleTask = {
Expand Down Expand Up @@ -113,8 +114,8 @@ const InteractionManagerStub = {
try {
task.run();
resolve();
} catch (error) {
reject(error);
} catch (error: unknown) {
reject(toError(error));
}
} else {
reject(new TypeError(`Task "${task.name}" missing gen or run.`));
Expand All @@ -123,8 +124,8 @@ const InteractionManagerStub = {
try {
task();
resolve();
} catch (error) {
reject(error);
} catch (error: unknown) {
reject(toError(error));
}
} else {
reject(new TypeError('Invalid task of type: ' + typeof task));
Expand Down
9 changes: 5 additions & 4 deletions packages/react-native/Libraries/LogBox/Data/LogBoxData.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
} from './parseLogBoxLog';

import DebuggerSessionObserver from '../../../src/private/devsupport/rndevtools/FuseboxSessionObserver';
import toExtendedError from '../../../src/private/utilities/toExtendedError';
import parseErrorStack from '../../Core/Devtools/parseErrorStack';
import NativeLogBox from '../../NativeModules/specs/NativeLogBox';
import LogBoxLog from './LogBoxLog';
Expand Down Expand Up @@ -240,8 +241,8 @@ export function addLog(log: LogData): void {
componentStackType: log.componentStackType || 'legacy',
}),
);
} catch (error) {
reportLogBoxError(error);
} catch (error: unknown) {
reportLogBoxError(toExtendedError(error));
}
});
}
Expand All @@ -252,8 +253,8 @@ export function addException(error: ExtendedExceptionData): void {
setImmediate(() => {
try {
appendNewLog(new LogBoxLog(parseLogBoxException(error)));
} catch (loggingError) {
reportLogBoxError(loggingError);
} catch (loggingError: unknown) {
reportLogBoxError(toExtendedError(loggingError));
}
});
}
Expand Down
9 changes: 5 additions & 4 deletions packages/react-native/Libraries/LogBox/LogBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {IgnorePattern, LogData} from './Data/LogBoxData';
import type {ExtendedExceptionData} from './Data/parseLogBoxLog';

import toExtendedError from '../../src/private/utilities/toExtendedError';
import Platform from '../Utilities/Platform';
import RCTLog from '../Utilities/RCTLog';
import * as React from 'react';
Expand Down Expand Up @@ -192,8 +193,8 @@ if (__DEV__) {
componentStackType,
});
}
} catch (err) {
LogBoxData.reportLogBoxError(err);
} catch (err: unknown) {
LogBoxData.reportLogBoxError(toExtendedError(err));
}
}
},
Expand Down Expand Up @@ -237,8 +238,8 @@ if (__DEV__) {
});
}
}
} catch (err) {
LogBoxData.reportLogBoxError(err);
} catch (err: unknown) {
LogBoxData.reportLogBoxError(toExtendedError(err));
}
};
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Network/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class XMLHttpRequest extends EventTarget {
case 'json':
try {
this._cachedResponse = JSON.parse(this._response);
} catch (_) {
} catch (_: unknown) {
this._cachedResponse = null;
}
break;
Expand Down
27 changes: 27 additions & 0 deletions packages/react-native/src/private/utilities/toError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

/**
* Converts an unknown value to an Error instance.
* If the value is already an Error, returns it as-is.
* Otherwise, creates a new Error with the stringified value as the message.
*
* This is particularly useful in catch blocks where the caught value
* is annotated as `unknown` but needs to be treated as an Error.
*
* @param value - The unknown value to convert to an Error
* @returns An Error instance
*/
export default function toError(value: unknown): Error {
if (value instanceof Error) {
return value;
}
return new Error(String(value));
}
30 changes: 30 additions & 0 deletions packages/react-native/src/private/utilities/toExtendedError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

import type {ExtendedError} from '../../../Libraries/Core/ExtendedError';

import toError from './toError';

/**
* Converts an unknown value to an ExtendedError instance suitable for LogBox.
* Uses the standard toError utility internally and then casts to ExtendedError.
*
* This is specifically designed for LogBox error reporting which requires
* ExtendedError type compatibility.
*
* @param value - The unknown value to convert to an ExtendedError
* @returns An ExtendedError instance
*/
export default function toExtendedError(value: unknown): ExtendedError {
const error = toError(value);
// ExtendedError extends Error, so this cast is safe for the LogBox system
// $FlowFixMe[incompatible-type] ExtendedError extends Error, this cast is safe
return (error: ExtendedError);
}
Loading