{"id":13412760,"url":"https://github.com/neuronlabs/errors","last_synced_at":"2026-02-17T19:12:30.235Z","repository":{"id":57495304,"uuid":"198917567","full_name":"neuronlabs/errors","owner":"neuronlabs","description":"Simple golang error handling with classification primitives.","archived":false,"fork":false,"pushed_at":"2019-08-02T15:28:00.000Z","size":38,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-31T20:51:23.948Z","etag":null,"topics":["classification","errors","golang","golang-error","interfaces","neuron"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/neuronlabs.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-07-26T00:15:36.000Z","updated_at":"2023-09-26T23:05:27.000Z","dependencies_parsed_at":"2022-08-31T12:52:02.322Z","dependency_job_id":null,"html_url":"https://github.com/neuronlabs/errors","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/neuronlabs/errors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neuronlabs%2Ferrors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neuronlabs%2Ferrors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neuronlabs%2Ferrors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neuronlabs%2Ferrors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neuronlabs","download_url":"https://codeload.github.com/neuronlabs/errors/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neuronlabs%2Ferrors/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29554546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T18:16:07.221Z","status":"ssl_error","status_checked_at":"2026-02-17T18:16:04.782Z","response_time":100,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":["classification","errors","golang","golang-error","interfaces","neuron"],"created_at":"2024-07-30T20:01:28.837Z","updated_at":"2026-02-17T19:12:30.218Z","avatar_url":"https://github.com/neuronlabs.png","language":"Go","readme":"![Neuron Logo](logo.svg)\n\n# Errors [![Go Report Card](https://goreportcard.com/badge/github.com/neuronlabs/errors)](https://goreportcard.com/report/github.com/neuronlabs/errors) [![GoDoc](https://godoc.org/github.com/neuronlabs/errors?status.svg)](https://godoc.org/github.com/neuronlabs/errors) [![Build Status](https://travis-ci.com/neuronlabs/errors.svg?branch=master)](https://travis-ci.com/neuronlabs/errors) [![Coverage Status](https://coveralls.io/repos/github/neuronlabs/errors/badge.svg?branch=master)](https://coveralls.io/github/neuronlabs/errors?branch=master) ![License](https://img.shields.io/github/license/neuronlabs/errors.svg)\n\nPackage errors provides simple golang error and classification primitives.\n\n* [Class](#class)\n* [Interfaces](#interfaces)\n* [Error Handling](#error-handling)\n* [Example](#example)\n* [Links](#links)\n\n## Class\n\nThe package defines blazingly fast classification system.\nA `Class` is an uint32 wrapper, composed of the `Major`, `Minor` and `Index` subclassifications.\nEach subclassifaction has different bitwise length.\nA major is composed of 8, minor 10 and index of 14 bits - total 32bits.\n\nExample:\n\n```Class with decimal value of 44205263, in a binary form equals to\n 00000010101000101000010011001111 which decomposes into:\n 00000010 - major (8 bit)\n         1010001010 - minor (10 bit)\n                   00010011001111 - index (14 bit)\n```\n\nThe class concept was inspired by the need of multiple errors\nwith the same logic but different messages.\n\nA class might be composed in three different ways:\n\n* Major only - the class is Major singleton.\n* Major, Minor only - classes that don't need triple subclassification divison.\n* Major, Minor, Index - classes that decomposes \n\nUse `NewMajor` or `MustNewMajor` functions to create `Major`, `NewMinor` or `MustNewMinor` for new `Minor` and `NewIndex` or `MustNewIndex` for new `Index`.\n\n\n\n## Interfaces\n\nThe package provides simple error handling interfaces and functions.\nIt allows to create simple and detailed classified errors.\n\n## ClassError\n\nA `ClassError` is the interface that provides error classification with the`Class` method.\n\n## DetailedError\n\n`DetailedError` is the interface used for errors that stores and handles human readable details, contains it's instance id and runtime call operation.\nImplements `ClassError`, `Detailer`, `Operationer`, `Indexer`, `error` interfaces.\n\n### Detailer\n\n`Detailer` interface allows to set and get the human readable details - full sentences.\n\n### Operationer\n\n`OperationError` is the interface used to get the runtime operation information.\n\n### Indexer\n\n`Indexer` is the interface used to obtain 'ID' for each error instance.\n\n\n## Error handling\n\nThis package contains two error structure implementations: \n\n* [Simple Error Structure](#simple-error)\n* [Detailed Error Structure](#detailed-error)\n\n### Simple Error\n\nA simple error implements `ClassError` interface. It is lightweight error that contains only a message and it's class.\n\nCreated by the `New` and `Newf` functions.\n\n**Example:**\n```go\nimport \"github.com/neuronlabs/errors\"\n// let's assume we have some ClassInvalidRequest already defined.\nvar ClassInvalidInput errors.Class\n\nfunc createValue(input int) error {\n    if input \u003c 0 {\n        return errors.New(ClassInvalidInput, \"provided input lower than zero\")\n    }\n\n    if input \u003e 50 {\n        return errors.Newf(ClassInvalidInput, \"provided input value: '%d' is not valid\", input) \n    }\n    // do the logic here\n    return nil\n}\n```\n\n### Detailed Error\n\nThe detailed error struct (`detailedError`) implements `DetailedError`.\n\nIt contains a lot of information about given error instance:\n\n* Human readable `Details`\n* Runtime function call `Operations`\n* Unique error instance `ID` \n\nIn order to create detailed error use the `NewDet` or `NewDetf` functions.\n\n### Example\n\n```go\nimport (\n    \"fmt\"\n    \"os\"\n\n    \"github.com/neuronlabs/errors\"\n)\n\nvar (\n    ClInputInvalidValue errors.Class\n    ClInputNotProvided  errors.Class\n)\n\nfunc init() {\n    initClasses()\n}\n\nfunc initClasses() {\n    inputMjr := errors.MustNewMajor()\n    invalidValueMnr := errors.MustNewMinor(inputMjr)\n    ClInputInvalidValue = errors.MustNewMinorClass(inputMjr, invalidValueMnr)\n    \n    inputNotProvidedMnr := errors.MustNewMinor(inputMjr)\n    ClInputNotProvided = errors.MustNewMinorClass(inputMjr, inputNotProvidedMnr)\n}\n\n\nfunc main() {\n    input, err := getInput()\n    if err == nil {\n        // Everything is fine.\n        os.Exit(0)\n    }\n\n    if classed, ok := err.(errors.ClassError); ok {\n        if classed.Class() == ClInputNotProvided {\n            fmt.Println(\"No required integer arguments provided.\")\n            os.Exit(1)\n        }\n    }\n\n    var details string\n    detailed, ok := err.(errors.DetailedError)\n    if ok {\n        details = detailed.Details()\n    } else {\n        details = err.Error()\n    }\n    fmt.Printf(\"Invalid input value provided: '%s'\\n\", details)\n    os.Exit(1)    \n}\n\n\nfunc checkInput(input int) error {\n    if input \u003c 0 {\n        err := errors.NewDet(ClassInputInvalidValue, \"provided input lower than zero\")        \n        err.SetDetailsf(\"The input value provided to the function is invalid. The value must be greater than zero.\")\n        return err\n    }\n\n    if input \u003e 50 {\n        err := errors.NewDetf(ClassInvalidInput, \"provided input value: '%d' is not valid\", input) \n        err.SetDetailsf(\"The input value: '%d' provided to the function is invalid. The value can't be greater than '50'.\", input)\n        return err\n    }\n    // do the logic here\n    return nil\n}\n\n\n\nfunc getInput() (int, error) {\n    if len(os.Args) == 0 {\n        return errors.New(ClInputNotProvided, \"no input provided\")\n    }\n\n    input, err := strconv.Atoi(os.Args[0])\n    if err != nil {\n        err := errors.NewDetf(ClInputInvalidValue, \"provided input is not an integer\")        \n        err.SetDetail(err.Error())\n        return 0, err\n    }\n\n    if err = checkInput(input); err != nil {\n        return 0, err\n    }\n    return input, nil\n}\n```\n\n## Links\n\n* [Neuron-Core](https://github.com/neuronlabs/neuron-core)\n* [Docs](https://docs.neuronlabs.io/errors)\n","funding_links":[],"categories":["Error Handling","错误处理","Relational Databases","错误处理`go 语言错误处理库`"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","Advanced Console UIs","SQL 查询语句构建库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneuronlabs%2Ferrors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneuronlabs%2Ferrors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneuronlabs%2Ferrors/lists"}