1+ import safeJSONStringify from 'safe-json-stringify' ;
2+
13type Nil = null | undefined ;
24/**
35 * 若有定义errorMessage为string,且data中包含errorMsg字段,则直接写入字段尝试替换错误信息
@@ -8,8 +10,8 @@ export function overwriteErrorMsgFieldIfSpecified(
810 data : ( Record < string , unknown > & { errorMsg ?: string | Nil } ) | Nil ,
911 errorMessage : string | Nil ,
1012) {
11- if ( typeof errorMessage === " string" && data ) {
12- if ( " errorMsg" in data ) {
13+ if ( typeof errorMessage === ' string' && data ) {
14+ if ( ' errorMsg' in data ) {
1315 data . errorMsg = errorMessage ;
1416 }
1517 }
@@ -18,26 +20,19 @@ export function overwriteErrorMsgFieldIfSpecified(
1820/**
1921 * stringfy 过滤掉 function 和循环引用
2022 * */
21- export function stringifyWithLoopProtection ( obj , replacer ?, space ?) {
22- const seen = new WeakSet ( ) ;
23+ export function stringifyWithLoopProtection ( obj : any , replacer ?: any , space ?: any ) {
2324 let hasCircleProp = false ;
24- const result = JSON . stringify (
25+
26+ const result = safeJSONStringify (
2527 obj ,
26- function ( key , value ) {
27- if ( typeof value === "object" && value !== null ) {
28- if ( seen . has ( value ) ) {
29- hasCircleProp = true ;
30- // 如果已经见过该对象,则返回undefined;
31- return undefined ;
32- }
33- seen . add ( value ) ;
34- }
35- // 使用用户提供的replacer函数(如果有的话)
36- if ( replacer ) {
37- return replacer ( key , value ) ;
38- } else {
39- return value ;
28+ ( key , value ) => {
29+ if ( value === '[Circular]' ) {
30+ // 循环引用
31+ hasCircleProp = true ;
32+ return undefined ;
4033 }
34+
35+ return replacer ( key , value ) ;
4136 } ,
4237 space ,
4338 ) ;
0 commit comments