Skip to content

Commit

Permalink
pass error stack at call stack through to warning
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-openspace committed Dec 3, 2024
1 parent f6e54f7 commit 1b3c104
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
21 changes: 13 additions & 8 deletions packages/react-native-reanimated/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function formatMessage(message: string) {
return `[Reanimated] ${message}`;
}

function createLog(level: LogBoxLogLevel, message: string): LogData {
function createLog(level: LogBoxLogLevel, message: string, errorOpts?: ErrorOpts): LogData {
'worklet';
const formattedMessage = formatMessage(message);

Expand All @@ -61,7 +61,7 @@ function createLog(level: LogBoxLogLevel, message: string): LogData {
componentStack: [],
componentStackType: null,
// eslint-disable-next-line reanimated/use-reanimated-error
stack: new Error().stack,
stack: errorOpts?.stack || new Error().stack,
};
}

Expand Down Expand Up @@ -119,10 +119,15 @@ type LogOptions = {
strict?: boolean;
};

type ErrorOpts = {
stack?: string;
}

function handleLog(
level: Exclude<LogBoxLogLevel, 'syntax' | 'fatal'>,
message: string,
options: LogOptions
options: LogOptions,
errorOpts?: ErrorOpts,
) {
'worklet';
const config = __reanimatedLoggerConfig;
Expand All @@ -140,16 +145,16 @@ function handleLog(
message += `\n\n${DOCS_REFERENCE}`;
}

config.logFunction(createLog(level, message));
config.logFunction(createLog(level, message, errorOpts));
}

export const logger = {
warn(message: string, options: LogOptions = {}) {
warn(message: string, options: LogOptions = {}, errorOpts: ErrorOpts = {}) {
'worklet';
handleLog('warn', message, options);
handleLog('warn', message, options, errorOpts);
},
error(message: string, options: LogOptions = {}) {
error(message: string, options: LogOptions = {}, errorOpts: ErrorOpts = {}) {
'worklet';
handleLog('error', message, options);
handleLog('error', message, options, errorOpts);
},
};
20 changes: 11 additions & 9 deletions packages/react-native-reanimated/src/mutables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ function shouldWarnAboutAccessDuringRender() {
return __DEV__ && isReactRendering() && !isFirstReactRender();
}

function checkInvalidReadDuringRender() {
function checkInvalidReadDuringRender(stack?: string) {
if (shouldWarnAboutAccessDuringRender()) {
logger.warn(
"Reading from `value` during component render. Please ensure that you don't access the `value` property nor use `get` method of a shared value while React is rendering a component.",
{ strict: true }
{ strict: true },
{ stack }
);
}
}

function checkInvalidWriteDuringRender() {
function checkInvalidWriteDuringRender(stack?: string) {
if (shouldWarnAboutAccessDuringRender()) {
logger.warn(
"Writing to `value` during component render. Please ensure that you don't access the `value` property nor use `set` method of a shared value while React is rendering a component.",
{ strict: true }
'!!Writing to `value` during component render. Please ensure that you do not access the `value` property or use `set` method of a shared value while React is rendering a component.',
{ strict: true },
{ stack }
);
}
}
Expand Down Expand Up @@ -149,14 +151,14 @@ function makeMutableNative<Value>(initial: Value): Mutable<Value> {

const mutable: PartialMutable<Value> = {
get value(): Value {
checkInvalidReadDuringRender();
checkInvalidReadDuringRender(Error().stack);
const uiValueGetter = executeOnUIRuntimeSync((sv: Mutable<Value>) => {
return sv.value;
});
return uiValueGetter(mutable as Mutable<Value>);
},
set value(newValue) {
checkInvalidWriteDuringRender();
checkInvalidWriteDuringRender(Error().stack);
runOnUI(() => {
mutable.value = newValue;
})();
Expand Down Expand Up @@ -205,11 +207,11 @@ function makeMutableWeb<Value>(initial: Value): Mutable<Value> {

const mutable: PartialMutable<Value> = {
get value(): Value {
checkInvalidReadDuringRender();
checkInvalidReadDuringRender(Error().stack);
return value;
},
set value(newValue) {
checkInvalidWriteDuringRender();
checkInvalidWriteDuringRender(Error().stack);
valueSetter(mutable as Mutable<Value>, newValue);
},

Expand Down

0 comments on commit 1b3c104

Please sign in to comment.