Skip to content

Commit

Permalink
feat: improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrati committed Jul 27, 2023
1 parent ff64bdb commit 394187f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export const open: Open = async (
openTimeout
)
} else if (socket.readyState == ReadyState.Closing) {
throw new Error('Socket is closing')
throw new Error('[maria2 error] Socket is closing')
} else if (socket.readyState == ReadyState.Closed) {
throw new Error('Socket is closed')
throw new Error('[maria2 error] Socket is closed')
}

socket.addEventListener('message', handleMessage)
Expand Down
34 changes: 28 additions & 6 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const isDev =
process.env['NODE_ENV'] != 'production' ||
(globalThis as any)?.Deno?.env?.get?.('NODE_ENV') != 'production'

export const isNodeEnv =
(globalThis as any)?.global?.process?.versions?.node != null

Expand All @@ -21,10 +25,28 @@ export const randomUUID = await (async () => {
if (nodeUUID != null) return nodeUUID
}

console.warn(
'[warn] Not found `crypto.randomUUID()` in this environment; ' +
'switching to fallback (unsafe)'
)
if (typeof Math.random == 'function') {
if (isDev) {
console.warn(
'[maria2 warn] Not found `crypto.randomUUID()` in this environment; ' +
'switching to fallback (Math.random)'
)
}

return () =>
[Math.random(), Math.random(), Math.random()]
.map((v) => v.toString(16).slice(2))
.join('')
.slice(0, 32)
.padStart(32, '0')
}

if (isDev) {
console.warn(
'[maria2 warn] Not found `crypto.randomUUID()` in this environment; ' +
'switching to fallback (unsafe counting)'
)
}

let count = 0
return () => (count += 1).toString()
Expand All @@ -43,7 +65,7 @@ export const useTimeout = <T>(
p,
sleep(ms).then(() => {
onTimeout?.()
throw new Error(`Timeout of ${ms}ms exceeded`)
throw new Error(`[maria2 error] Timeout of ${ms}ms exceeded`)
}),
])

Expand All @@ -68,6 +90,6 @@ export const decodeMessageData = (data: any) => {
}
return out.join('')
} else {
throw new Error('Data cannot be decoded')
throw new Error('[maria2 error] Message data cannot be decoded')
}
}
2 changes: 1 addition & 1 deletion src/transport/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const createPost = await (async () => {
return (await import('../shims/xhr.ts')).xhrPost
} else {
return () => {
throw new Error('Not found any http client implementation.')
throw new Error('[maria2 error] HTTP client implementation is missing')
}
}
})()
Expand Down
6 changes: 4 additions & 2 deletions src/transport/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const WebSocket =
? (await import('ws')).default
: class {
constructor() {
throw new Error('Not found any http client implementation.')
throw new Error(
'[maria2 error] WebSocket client implementation is missing'
)
}
})

Expand All @@ -29,7 +31,7 @@ export const createWebSocket: CreateWebSocket = (
options?: Partial<OpenOptions>
) => {
if (WebSocket == null) {
throw new Error('Not Found `WebSocket()` in globalThis or require()')
throw new Error('[maria2 error] WebSocket client implementation is missing')
}

return new (class extends WebSocket {
Expand Down

0 comments on commit 394187f

Please sign in to comment.