{"id":16228528,"url":"https://github.com/poppa/assert-error","last_synced_at":"2026-04-16T22:31:01.651Z","repository":{"id":51344257,"uuid":"349551278","full_name":"poppa/assert-error","owner":"poppa","description":"Assert or type assert that error is an Error","archived":false,"fork":false,"pushed_at":"2022-08-18T07:09:55.000Z","size":298,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-15T01:55:44.931Z","etag":null,"topics":["assertions","error-handling","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/poppa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-19T20:46:46.000Z","updated_at":"2022-08-18T06:32:40.000Z","dependencies_parsed_at":"2022-09-24T21:21:07.874Z","dependency_job_id":null,"html_url":"https://github.com/poppa/assert-error","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/poppa/assert-error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poppa%2Fassert-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poppa%2Fassert-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poppa%2Fassert-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poppa%2Fassert-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/poppa","download_url":"https://codeload.github.com/poppa/assert-error/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poppa%2Fassert-error/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31907424,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"ssl_error","status_checked_at":"2026-04-16T18:21:47.142Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assertions","error-handling","typescript"],"created_at":"2024-10-10T12:55:41.988Z","updated_at":"2026-04-16T22:31:01.603Z","avatar_url":"https://github.com/poppa.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# assert-error\n\nWhen using strict errors in TS the type of the error in the `catch` clause\nis `unknown`. This is a good thing since it forces you to properly handle the\nerror.\n\nThis package provides two functions to make this error handling easier.\n\n## Install\n\nThe usual `npm install @poppanator/assert-error`,\n`yarn add @poppanator/assert-error`, etc...\n\n## Example\n\nPonder the following\n\n```ts\ntry {\n  // Do some funky salsa\n} catch (err: unknown) {\n  console.error('Error message:', err.message)\n  // .................................^^^^^^^\n  // It's a nono\n}\n```\n\nSince TS doesn't know wether `err` is an error object or not you can't index\non it.\n\nBut using `assertError()` you can:\n\n```ts\nimport { assertError } from '@poppanator/assert-error'\n\ntry {\n  // Do some funky chimichurri\n} catch (err: unknown) {\n  assertError(err)\n  console.error('Error message:', err.message)\n  // It's a Yay-Yay\n}\n```\n\nNow TS known ist's an Error object and indexing on it is fine.\nIf `err` is not an instance of (at least) `Error`, `assertError()` will throw.\n\nTo assert `err` is some other error than `Error`, you can provide the class\nconstructor as second argument to `assertError()`:\n\n```ts\nclass MyError extends Error {\n  public code = 12\n}\n\ntry {\n  // Do some funky pesto\n} catch (err: unknown) {\n  assertError(err, MyError)\n  console.error('Error:', err.code)\n}\n```\n\nNow, you may not always want something to throw in you `catch` clause. If\nthat's the case you can use `assumeError()`, which is just a typeguard working\nin the same way as `assertError()`.\n\n`assertError()` is to prefer over `assumeError()`, but there might be situations\nwhere you just want to type-guard and not risking ending up with something\nthrowing an error.\n\n**NOTE!** `assumeError()` has no runtime-effect. It does nothing.\n\n```ts\nimport { assumeError } from '@poppanator/assert-error'\n\ntry {\n  // Do some funky gaspacho\n} catch (err: unknown) {\n  assumeError(err)\n  console.error('Error:', err.message)\n\n  // These two are the same thing\n  assumeError\u003cMyError\u003e(err)\n  assumeError(err, MyError)\n\n  if (err.code) {\n    console.error('Error code:', err.code)\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoppa%2Fassert-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpoppa%2Fassert-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoppa%2Fassert-error/lists"}