{"id":22747231,"url":"https://github.com/one-com/failraft","last_synced_at":"2025-03-30T05:40:40.918Z","repository":{"id":57232447,"uuid":"223427635","full_name":"One-com/failraft","owner":"One-com","description":null,"archived":false,"fork":false,"pushed_at":"2019-11-23T13:00:23.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-04-26T08:03:02.080Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/One-com.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-11-22T15:05:42.000Z","updated_at":"2019-11-23T13:00:25.000Z","dependencies_parsed_at":"2022-08-31T14:10:46.396Z","dependency_job_id":null,"html_url":"https://github.com/One-com/failraft","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Ffailraft","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Ffailraft/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Ffailraft/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Ffailraft/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/One-com","download_url":"https://codeload.github.com/One-com/failraft/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246281218,"owners_count":20752207,"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":[],"created_at":"2024-12-11T03:13:49.532Z","updated_at":"2025-03-30T05:40:40.902Z","avatar_url":"https://github.com/One-com.png","language":"JavaScript","readme":"# Failraft\n\nThis module extends [`Failboat`](https://github.com/One-com/failboat) to\nmake it work in conjunction with redux.\n\nError handlers registered with Failraft are dispatched via the store to which\nit is attached - this essentialy allows actions to be triggered in response\nto error conditions in the application.\n\n## Usage\n\nOnce imported the module is instantiated with an object consisting of handlers\nto be executed when it consumes an error.\n\n## Configuring error handlers\n\nThe handler object is keyed by strings that match any \"tags\" that are attached\nto the error, while tagging itself is customisable by supplying a function that\ncan be used to decode the error and return an array of appropriate tags.\n\nLet's look at an example:\n\n```js\nconst Failraft = require(\"failraft\");\n\nconst instanceOptions = {\n  determineErrorTags: error =\u003e [error.name]\n};\n\nconst instance = new Failraft({\n  Error: error =\u003e ({ type: \"WARNING_ACTION\", payload: error.message }),\n  SyntaxError: error =\u003e ({ type: \"ALARM_ACTION\", payload: error.message })\n});\n```\n\nHere we configure Failraft to use the error name when looking up which\nerror handler to fire. If we have code that catches invalid JSON responses\nfrom the server (a SyntaxError is typically thrown when parsing such data)\nwe can arrange two different messages to be shown in this case.\n\n## Attaching a store\n\nAs you might have noticed, the handlers we declared above are actually redux\nactions. In an appliction that uses redux, this is important because changes\nin state are represented by actions and applied by reducers.\n\nIn order to \"activate\" the error handler, it must be linked to the application\nredux store. There are two methods are provided for doing this:\n\n### attachStore()\n\nThis hints the Failraft instance that actions should be dispatched via a\nparticular store:\n\n```js\nconst redux = require(\"redux\");\nconst store = redux.createStore(state =\u003e state, {});\n\nnew Failraft({\n  /* error routes */\n}).attachStore(store);\n```\n\n### createReduxMiddleware()\n\nThis method returns a middleware that is suitable for direct inclusion as\na middleware in the store:\n\n```js\nconst failraftInstance = new Failraft({\n  /* error routes */\n});\n\nconst { createStore, applymiddleware } = require(\"redux\");\nconst storeWithFailraftMiddleware = redux.createStore(\n  state =\u003e state,\n  {},\n  applyMiddleware(failraftInstance.createReduxMiddleware())\n);\n```\n\nThe middleware watches actions passing through the store and any that are\nidenfied as errors will be passed into Failraft for handling.\n\nCurrently, error actions can be of any type but the dispatched object must\nhave the following to properties:\n\n- error: \u0026lt;error object\u0026gt;\n- errorAction: true\n\nIn practice, that actions representing errors look something like:\n\n```js\n{ type: 'SOME_FAILURE', error: new Error(), errorAction: true }\n```\n\nIf actions that count as errors have a different structure in your redux\nstore, the `identifyErrorAction` function can be supplied to the middleware\nto customise this. The example below matches error actions by type name:\n\n```js\n{ type: '@ERROR/some_condition', error: new Error() }\n\nconst customMiddleware = new Failraft({\n  /* error routes */\n}).createReduxMiddleware({\n  identifyErrorAction: action =\u003e action.type.startsWith('@ERROR/')\n})\n```\n\n## Triggering errors\n\nIn order for an error error handled correctly we must provide Failraft with a\nfunction that is able to decode an error to a set of \"tags\" that represent it\nwhich are used to discover the correct handler. We do this by including the\n`determineErrorTags` function in the second options argument.\n\nAs in our first example, in order to use the name of an error as a match against\nhandlers we would return it as follows:\n\n```js\nnew Failraft(\n  {\n    /* error routes */\n  },\n  {\n    determineErrorTags: error =\u003e [error.name]\n  }\n);\n```\n\nThe only requirement placed on determineErrorTags() is that is returns an\narray contain single strings that will be matched.\n\nError can be directly passed for handling by calling the `consumeError(error)`\nmethod, attached to the Failraft instance, while users of the middleware will\nhave this automitically arranged for them.\n\n### Extended error handlers\n\nThere are cases where, in a particular situation, some custom handling is\nrequired - perhaps a message being shown that is specific for one portion\nof the application.\n\nThis can be achieved by including additional error handlers on the action\nbeing dispatched - the middleware will match these first and the handler\nfired is successful. If none is found we fall back and attempt to find a\nmatch in the globally registered handlers.\n\n```js\nconst failureAction = error =\u003e ({\n  type: \"ACTION_TYPE_ON_ERROR\",\n  error,\n  errorAction: true,\n  additionalErrorHandlers: {\n    Error: () =\u003e ({})\n  }\n});\n\nstoreWithFailraftMiddleware.dispatch(failureAction(new Error(\"some failure\")));\n```\n\n## License\n\nFailraft is licensed under a standard 3-clause BSD\nlicense -- see the `LICENSE`-file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fone-com%2Ffailraft","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fone-com%2Ffailraft","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fone-com%2Ffailraft/lists"}