Skip to content

Commit ac683c7

Browse files
committed
feat: improve stringifying unknown variable
1 parent 0860e27 commit ac683c7

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/error.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AxiosError } from 'axios'
22
import { CustomError } from 'ts-custom-error'
33

4-
import { logger, stringify } from '@/utils'
4+
import { logger, stringifyForDebugging } from '@/utils'
55

66
export const ERR_UPLOADING_PACKAGE = 1
77
export const ERR_PUBLISHING_PACKAGE = 4
@@ -25,11 +25,11 @@ export function tryGetErrorMessage(e: unknown): string {
2525
if (typeof e === 'object' && e !== null && 'message' in e && typeof e.message === 'string') {
2626
return e.message
2727
}
28-
return stringify(e)
28+
return stringifyForDebugging(e)
2929
}
3030

3131
export function getStringOrError(e: unknown): string | Error {
32-
return e instanceof Error ? e : stringify(e)
32+
return e instanceof Error ? e : stringifyForDebugging(e)
3333
}
3434

3535
export function handleError(error: unknown): never {
@@ -62,7 +62,7 @@ export function handleError(error: unknown): never {
6262
}
6363

6464
// Unknown error. This may be a bug of this action.
65-
let str_err = stringify(error)
65+
let str_err = stringifyForDebugging(error)
6666
if (str_err.length > 256) {
6767
str_err = `${str_err.slice(0, 256)} <truncated>`
6868
}

src/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import { globSync } from 'glob'
55

66
import { ERR_INVALID_INPUT, EdgeAddonActionError } from '@/error'
77

8-
export function stringify(e: unknown): string {
8+
export function stringifyForDebugging(e: unknown): string {
99
if (typeof e === 'object') {
1010
return JSON.stringify(e)
1111
}
1212
if (typeof e === 'string') {
13-
return e
13+
return e.trim() ? e : '<empty string>'
1414
}
15-
return String(e)
15+
16+
// Since e is not object, we can safely call String(e).
17+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
18+
const ret = String(e)
19+
return ret.trim() ? ret : '<empty string>'
1620
}
1721

1822
export function tryResolveFile(pattern: string): string {

test/utils.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import tmp from 'tmp'
55
import { describe, expect, test } from 'vitest'
66

77
import { ERR_INVALID_INPUT, EdgeAddonActionError } from '@/error'
8-
import { stringify, tryResolveFile } from '@/utils'
8+
import { stringifyForDebugging, tryResolveFile } from '@/utils'
99

1010
test('stringify', () => {
11-
expect(stringify('hello')).toBe('hello')
12-
expect(stringify(42)).toBe('42')
13-
expect(stringify([1, 2, 3])).toBe('[1,2,3]')
14-
expect(stringify({ foo: 'bar' })).toBe('{"foo":"bar"}')
15-
expect(stringify(null)).toBe('null') // eslint-disable-line unicorn/no-null
16-
expect(stringify(undefined)).toBe('undefined')
11+
expect(stringifyForDebugging('hello')).toBe('hello')
12+
expect(stringifyForDebugging(42)).toBe('42')
13+
expect(stringifyForDebugging([1, 2, 3])).toBe('[1,2,3]')
14+
expect(stringifyForDebugging({ foo: 'bar' })).toBe('{"foo":"bar"}')
15+
expect(stringifyForDebugging(null)).toBe('null') // eslint-disable-line unicorn/no-null
16+
expect(stringifyForDebugging(undefined)).toBe('undefined')
1717
})
1818

1919
describe('tryResolveFile', () => {

0 commit comments

Comments
 (0)