Fetching JSON with Error Handling and Logging in TypeScript
import { NodeRuntime } from "@effect/platform-node"
import { Console, Effect } from "effect"
class Err extends Error {
constructor(
message: string,
readonly status: number
) {
super(message)
}
}
const getJson = (url: string) =>
Effect.tryPromise({
try: () =>
fetch(url).then((res) => {
if (!res.ok) {
console.log(res.status)
throw new Err(res.statusText, res.status)
}
return res.json() as unknown
}),
catch: (e) => e as Err
})
const program = (url: string) =>
getJson(url).pipe(
Effect.tap((response) => Console.log(response)),
// Retry if the error is a 403
Effect.retry({ while: (err) => err.status === 403, times: 3 }),
Effect.catchAll(Console.error)
)
// testing 403
NodeRuntime.runMain(
program("https://dummyjson.com/auth/products/1?delay=1000")
)import { NodeRuntime } from "@effect/platform-node"
import { Console, Effect } from "effect"
class Err extends Error {
constructor(
message: string,
readonly status: number
) {
super(message)
}
}
const getJson = (url: string) =>
Effect.tryPromise({
try: () =>
fetch(url).then((res) => {
if (!res.ok) {
console.log(res.status)
throw new Err(res.statusText, res.status)
}
return res.json() as unknown
}),
catch: (e) => e as Err
})
const program = (url: string) =>
getJson(url).pipe(
Effect.tap((response) => Console.log(response)),
// Retry if the error is a 403
Effect.retry({ while: (err) => err.status === 403, times: 3 }),
Effect.catchAll(Console.error)
)
// testing 403
NodeRuntime.runMain(
program("https://dummyjson.com/auth/products/1?delay=1000")
)why is tap log not working
