Skip to content

Commit

Permalink
fix: 优化parseRid方法
Browse files Browse the repository at this point in the history
  • Loading branch information
jay committed May 1, 2022
1 parent 85ec14b commit 841b462
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export function createNonceStr (length = 16): string {
* @returns
*/
export function parseRid (errMsg: string): string {
if (typeof errMsg !== 'string') {
return '';
}
const index = errMsg.indexOf('rid:');
return errMsg.substring(index + 5);
if (index >= 0) {
return errMsg.substring(index + 5);
} else {
return '';
}
}
3 changes: 3 additions & 0 deletions lib/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ describe('Test utils', () => {
const errMsg = `invalid credential, access_token is invalid or not latest rid: ${rid}`;
const result = parseRid(errMsg);
expect(result).toStrictEqual(rid);

expect(parseRid({} as any)).toStrictEqual('');
expect(parseRid('no rid to parse')).toStrictEqual('');
});

});

0 comments on commit 841b462

Please sign in to comment.