{"id":16099243,"url":"https://github.com/drjume/try-inline","last_synced_at":"2025-03-18T07:30:58.074Z","repository":{"id":57380383,"uuid":"127419584","full_name":"DrJume/try-inline","owner":"DrJume","description":"An easy inline error handling wrapper for async promises and syncronous functions","archived":false,"fork":false,"pushed_at":"2018-06-19T10:32:07.000Z","size":17,"stargazers_count":24,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-01T19:15:22.348Z","etag":null,"topics":["async","await","logging","try","try-catch"],"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/DrJume.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":"2018-03-30T11:17:53.000Z","updated_at":"2019-02-14T20:15:22.000Z","dependencies_parsed_at":"2022-09-05T13:51:29.226Z","dependency_job_id":null,"html_url":"https://github.com/DrJume/try-inline","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/DrJume%2Ftry-inline","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrJume%2Ftry-inline/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrJume%2Ftry-inline/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrJume%2Ftry-inline/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DrJume","download_url":"https://codeload.github.com/DrJume/try-inline/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243910755,"owners_count":20367545,"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":["async","await","logging","try","try-catch"],"created_at":"2024-10-09T18:26:30.111Z","updated_at":"2025-03-18T07:30:57.735Z","avatar_url":"https://github.com/DrJume.png","language":"JavaScript","readme":"# :traffic_light: TryInline\n\u003e An easy inline error handling wrapper for async promises and syncronous functions \n\n[![NPM Version][npm-image]][npm-url]\n\n:bulb: _Inspired by [await-to-js](https://github.com/scopsy/await-to-js)_\n\n## :gift: Example\n```js\nconst { try_ } = require('try-inline');\n\nlet err, data;\n\n// async\n[err, data] = await try_(somePromise());\nif (err) process.exit(1);\nconsole.log(data);\n\n// normal function\n[err, data] = try_(() =\u003e someFunction());\nif (err) process.exit(1);\nconsole.log(data);\n```\n\n## :package: Installation\n```bash\n$ npm install try-inline\n```\n\n## :barber: Features\n  - Inline error catching\n  - Configurable error logging\n  - Error object patching\n    - on a execution fail, the returned error object includes its _ErrorString_\n  - Labeling executions for better debugging\n  - Filtering error results for specified key-paths\n  (documentation coming soon)\n    - only show specific keys from the error object\n\n## :nut_and_bolt: API\n\n### `try_(executionObj, logOptionsString, [options]) =\u003e [err, data]`\n\nWraps an execution safely. The default TryInline instance. \n- `executionObj` - the object to execute. Can be a **promise** or a **callback with a syncronous function**.\n- `logOptionsString` - *optional* (you can leave it empty) option string for the logger.\n  - Format: `\"(logLevel:)labelString\"`\n    - `logLevel` - method used from logger. The default logger is the JavaScript global \"console\". So the available values are: _`info, log, warn, error`_. **Defaults** to `error`. When you want to use your own logger, take a look at creating your own TryInline custom instance.\n    - `labelString` - optional label attached to the error log message. \n  - Example: `\"warn:HTTP_TIMEOUT\"` -\u003e Logger gets the '*warn*' log-level and the label string '*HTTP_TIMEOUT*'\n- `options` - optional object with:\n  - `errData` - additional error information (assinged to `error.ErrorData`).\n\n**Returns** an array with two values:\n- `err` - the error obejct. When `executionObj` throws an error, it is assigned to `err`. Otherwise `err` is **null**.\n- `data` - returned value from `executionObj`. On error it gets **undefined**.\n\n```js\nconst { try_ } = require('try-inline');\n\nlet [err, data] = await try_(readFilePromise('lorem.txt'), 'warn:READ_FILE_ERR',\n    { errData: \"Safely ignore the error. The lorem file is optional.\" } \n});\n\n// array destructuring is awesome!\nlet [err]    = ... // just get the error obj\nlet [, data] = ... // only get the data obj\n```\n\n### `new TryInline(options) =\u003e try_ (customized)`\n\nCreates a custom TryInline instance with specified options.\n- `options` - required object where:\n  - `Logger` - custom error handling function. It gets _`error, level, label`_ passed as arguments.\n  - `DefaultLogLevel` - set the default level for your Logger.\n\n**Returns** a custom *`try_`* instance with attached `Logger`.\n\n```js\nconst TryInline = require('try-inline');\n\nconst try_ = new TryInline({\n  Logger: function(error, level, label) {\n    const logMessage = label ? `(${label}) ${error}` : error;\n    console[level](logMessage);\n  },\n  DefaultLogLevel: 'debug'\n});\n```\n\n## :point_up: Notes\nDo not always trust automatic semi-colon insertion (ASI) in JavaScript, when not using semi-colons! \nBe careful when assigning the output variables by destructuring the returned array!\n\nWhen you want to be `100% safe`, then put a semi-colon in front of the destructuring statement:\n```js\n;[err, data] = await try_(somePromise())\n```\n\n## :page_with_curl: License\n[MIT](https://github.com/DrJume/try-inline/blob/master/LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/try-inline.svg\n[npm-url]: https://www.npmjs.com/package/try-inline\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrjume%2Ftry-inline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrjume%2Ftry-inline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrjume%2Ftry-inline/lists"}