{"id":20910574,"url":"https://github.com/aegatlin/eor","last_synced_at":"2025-05-13T07:31:48.860Z","repository":{"id":57226139,"uuid":"210243839","full_name":"aegatlin/eor","owner":"aegatlin","description":"eor is a try-catch wrapper","archived":false,"fork":false,"pushed_at":"2022-09-05T07:04:38.000Z","size":20,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-05T09:21:17.172Z","etag":null,"topics":["error-handling","javascript","nodejs","npm","npm-package","try-catch","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/eor","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/aegatlin.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":"2019-09-23T01:56:06.000Z","updated_at":"2022-09-08T03:09:57.000Z","dependencies_parsed_at":"2022-08-24T11:00:39.771Z","dependency_job_id":null,"html_url":"https://github.com/aegatlin/eor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aegatlin%2Feor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aegatlin%2Feor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aegatlin%2Feor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aegatlin%2Feor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aegatlin","download_url":"https://codeload.github.com/aegatlin/eor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225188271,"owners_count":17435032,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["error-handling","javascript","nodejs","npm","npm-package","try-catch","typescript"],"created_at":"2024-11-18T14:16:03.956Z","updated_at":"2024-11-18T14:16:04.635Z","avatar_url":"https://github.com/aegatlin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# eor\n\nEor is a strongly-typed try-catch wrapper library that makes `try`ing and `catch`ing easier to work with. It comes with two functions, `eor`, and `nor`.\n\n```sh\nnpm install eor\n```\n\n```js\nimport { eor } from 'eor'\n\nlet [err, num] = eor(() =\u003e 1) // =\u003e [null, 1]\nlet [err, num] = eor(() =\u003e {\n  throw 'bad'\n}) // =\u003e ['bad', null]\n```\n\n```js\nimport { nor } from 'eor'\n\nlet num = nor(() =\u003e 1) // =\u003e 1\nlet num = nor(() =\u003e {\n  throw 'bad'\n}) // =\u003e null\n```\n\n## Explanation\n\nTry-catch code is noisy and hard to work with. This is a recipe for bugs. Simplifying the interface will lead to that is easier to read and reason about. With `nor` you get the simplest experience: you either get the return value, or you get `null`.\n\n```js\nconst x = nor(() =\u003e 1) // =\u003e 1\nconst y = nor(() =\u003e {\n  throw 'bad'\n}) // =\u003e null\n```\n\nWith `eor` you get more to work with, in that the return value is wrapped in an error-data tuple, which lets you use the error value in your control-flow.\n\n```js\nimport { eor } from 'eor'\n\nlet [err, num] = eor(() =\u003e 1) // =\u003e [null, 1]\nlet [err, num] = eor(() =\u003e {\n  throw 'bad'\n}) // =\u003e ['bad', null]\n```\n\n## Tutorials\n\n### Query Params to Database Query\n\nLet's say you are passing query params to a database query (safely!). You don't know ahead of time whether the `id` provided in the query param exists. Your database library throws when it fails to find the row. Also, all of this is asynchronous.\n\nFirst, install eor `npm i eor`. Then wrap your database call in `eor`. `queryParamId` might be undefined, or it might be defined and not exist, both of which will cause our database library to throw.\n\n```js\nimport { eor } from 'eor'\nimport { db } from 'db-library'\n\n// assume the db-library throws the following strings when it throws.\nfunction get(queryParamId) {\n  const [e, data] = await eor(() =\u003e db.get(queryParamId))\n  if (e === 'id is undefined') {\n    return 500\n  } else if (e === 'id does not exist') {\n    return 400\n  }\n\n  return data\n}\n```\n\nAlternatively, with `nor` you can simply try to get the data, and bail on failure.\n\n```js\nimport { nor } from 'eor'\nimport { db } from 'db-library'\n\nfunction get(queryParamId) {\n  const data = await nor(() =\u003e db.get(queryParamId))\n  return data ? data : 404\n}\n```\n\n### Delicate Calculator\n\nLet's say you are working with a library that performs a delicate and complicated calculation. You want to take user input and try to perform the calculation.\n\nFirst, install eor `npm i eor`. Then, wrap the calculation in `eor`. If it is successful we will return the results, otherwise we will log the error as a warning and return the error value in case a function upstream wants to work with it.\n\n```js\nimport { eor } from 'eor'\nimport { calculator } from 'delicate-complicated-library'\n\nfunction safeCalculator(userInput1, userInput2) {\n  const [e, results] = eor(() =\u003e calculator(userInput1, userInput2))\n  if (results) return results\n  console.warn(e)\n  return e\n}\n```\n\nIf we didn't care about the errors coming from the library itself, we could instead use `nor`.\n\n```js\nimport { nor } from 'eor'\nimport { calculator } from 'delicate-complicated-library'\n\nfunction safeCalculator(userInput1, userInput2) {\n  const results = nor(() =\u003e calculator(userInput1, userInput2))\n  if (results) return results\n  console.warn('calculation failed!')\n}\n```\n\n## How-To Guides\n\n### Work with Error-Data Tuples\n\n```js\nfunction mightThrow() {\n  // code which might throw...\n}\n\nfunction bad(e) {\n  // what to do when `mightThrow` throws...\n}\n\nfunction good(data) {\n  // what to do when `mightThrow` returns data...\n}\n\nfunction f() {\n  const [e, data] = eor(mightThrow)\n  if (e) return bad(e)\n  return good(data)\n}\n```\n\n### Working with functions with parameters\n\nYou can call functions with parameters by wrapping it in a higher-order function with **no** parameters.\n\n```js\nconst a = 1\nconst b = 2\nconst multiParameterFunction = (a, b) =\u003e a + b\nconst x = nor(() =\u003e multiParameterFunction(a, b))\n// x === 3\n```\n\n## Reference\n\n### eor\n\n`eor` stands for \"Error OR\". It has one parameter, either a function with **no** parameters, or a Promise. In all cases it will `try` to return the results. If it succeeds, for return type `R`, it returns `[null, R]`. If it fails, it returns `[any, null]`, where `any` is the returned error value.\n\n#### Synchronous Functions\n\n```js\nconst [e, num] = eor(() =\u003e 1) // =\u003e [null, 1]\n```\n\n```js\nconst [e, num] = eor(() =\u003e {\n  throw 'bad'\n}) // =\u003e ['bad', null]\n```\n\n#### Asynchronous Functions\n\n```js\nasync function fetchData {\n  // async stuff...\n  return 1\n}\nconst [e, num] = await eor(fetchData) // =\u003e [null, 1]\n```\n\n```js\nasync function fetchData {\n  throw 'bad'\n}\nconst [e, num] = await eor(fetchData) // =\u003e ['bad', null]\n```\n\n#### Promise\n\n```js\nconst p = Promise.resolve(1)\nconst [e, num] = await eor(p) // =\u003e [null, 1]\n```\n\n```js\nconst p = Promise.reject('bad')\nconst [e, num] = await eor(p) // =\u003e ['bad', 1]\n```\n\n### nor\n\n`nor` stands for \"Null OR\". It has one parameter, either a function with **no** parameters, or a Promise. In all cases it will `try` to return the results. If it succeeds, for return type `R`, it returns `R`. If it fails, it returns `null`.\n\n#### Synchronous Functions\n\n```js\nconst num = nor(() =\u003e 1) // =\u003e 1\n```\n\n```js\nconst num = nor(() =\u003e {\n  throw 'bad'\n}) // =\u003e null\n```\n\n#### Asynchronous Functions\n\n```js\nasync function fetchData {\n  // async stuff...\n  return 1\n}\nconst num = await nor(fetchData) // =\u003e 1\n```\n\n```js\nasync function fetchData {\n  throw 'bad'\n}\nconst num = await nor(fetchData) // =\u003e null\n```\n\n#### Promise\n\n```js\nconst p = Promise.resolve(1)\nconst num = await nor(p) // =\u003e 1\n```\n\n```js\nconst p = Promise.reject('bad')\nconst num = await nor(p) // =\u003e null\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faegatlin%2Feor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faegatlin%2Feor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faegatlin%2Feor/lists"}