mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
d544fa20b7
Summary: Error objects logged as part of the arguments to `console.error` such as from [rejected es6 promises](https://github.com/zloirock/core-js/blob/v2/modules/es6.promise.js#L110) contain the error that the user would want to see as the error object's message, but is not captured by `stringifySafe`. Here we modify it to if the logged value is an error object print the error similar to chrome: ``` const error = new Error('error'); stringifySafe(error); // Error: error ``` Versus the current behavior which does not recognize the error type and instead tries to stringify the it as an object: ``` JSON.stringify(new Error('error')) // "{}" ``` ## Changelog [JavaScript] [Changed] - Add support for stringifying error object messages to safeStringify Pull Request resolved: https://github.com/facebook/react-native/pull/25723 Test Plan: Tests: <img width="802" alt="Screen Shot 2019-07-18 at 8 39 52 PM" src="https://user-images.githubusercontent.com/2192930/61501171-39218080-a99c-11e9-8e87-48ea413b3d01.png"> Lint: <img width="406" alt="Screen Shot 2019-07-18 at 8 43 35 PM" src="https://user-images.githubusercontent.com/2192930/61501318-dc729580-a99c-11e9-9264-c0232515352c.png"> Differential Revision: D16437956 Pulled By: cpojer fbshipit-source-id: ca3ce9c98ad585beb29c2bfeb81bbd14b2b1c700
57 lines
1.6 KiB
JavaScript
57 lines
1.6 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @emails oncall+react_native
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
describe('stringifySafe', () => {
|
|
const stringifySafe = require('../stringifySafe');
|
|
|
|
it('stringifySafe stringifies undefined values', () => {
|
|
expect(stringifySafe(undefined)).toEqual('undefined');
|
|
});
|
|
|
|
it('stringifySafe stringifies null values', () => {
|
|
expect(stringifySafe(null)).toEqual('null');
|
|
});
|
|
|
|
it('stringifySafe stringifies string values', () => {
|
|
expect(stringifySafe('abc')).toEqual('"abc"');
|
|
});
|
|
|
|
it('stringifySafe stringifies function values', () => {
|
|
expect(stringifySafe(function() {})).toEqual('function () {}');
|
|
});
|
|
|
|
it('stringifySafe stringifies non-circular objects', () => {
|
|
expect(stringifySafe({a: 1})).toEqual('{"a":1}');
|
|
});
|
|
|
|
it('stringifySafe stringifies circular objects with toString', () => {
|
|
const arg = {};
|
|
arg.arg = arg;
|
|
const result = stringifySafe(arg);
|
|
expect(result).toEqual('[object Object]');
|
|
});
|
|
|
|
it('stringifySafe stringifies circular objects without toString', () => {
|
|
const arg = {};
|
|
arg.arg = arg;
|
|
arg.toString = undefined;
|
|
const result = stringifySafe(arg);
|
|
expect(result).toEqual('["object" failed to stringify]');
|
|
});
|
|
|
|
it('stringifySafe stringifies error messages', () => {
|
|
const error = new Error('error');
|
|
const result = stringifySafe(error);
|
|
expect(result).toEqual('Error: error');
|
|
});
|
|
});
|