Skip to content

Commit

Permalink
test: isErrnoException
Browse files Browse the repository at this point in the history
  • Loading branch information
stefreak committed Oct 31, 2023
1 parent 2040ca2 commit 2485bb1
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions core/test/unit/src/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,55 @@ import {
RuntimeError,
StackTraceMetadata,
getStackTraceMetadata,
isErrnoException,
} from "../../../src/exceptions"
import dedent from "dedent"
import { testFlags } from "../../../src/util/util"
import { readFile } from "fs-extra"
import { resolve4 } from "dns/promises"
import dns from "node:dns"

describe("isErrnoException", async () => {
it("should return true for file not found errors", async () => {
let err: unknown

try {
await readFile("non-existent-file")
expect.fail("should have thrown")
} catch (e) {
err = e
}

if (isErrnoException(err)) {
expect(err.code).to.equal("ENOENT")
} else {
expect.fail("should have been an NodeJSErrnoException")
}
})

it("should return true for DNS ENOTFOUND errors", async () => {
let err: unknown

try {
await resolve4("non-existent-hostname")
expect.fail("should have thrown")
} catch (e) {
err = e
}

if (isErrnoException(err)) {
expect(err.code).to.equal(dns.NOTFOUND)
expect(dns.NOTFOUND).to.equal("ENOTFOUND") // for sanity
} else {
expect.fail("should have been an NodeJSErrnoException")
}
})

it("should return false for other errors", () => {
const err = new Error("test exception")
expect(isErrnoException(err)).to.be.false
})
})

describe("GardenError", () => {
// helper to avoid dealing with changing line numbers
Expand Down

0 comments on commit 2485bb1

Please sign in to comment.