Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfsp committed Sep 25, 2021
0 parents commit 6e6370d
Show file tree
Hide file tree
Showing 16 changed files with 910 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Default owners
@thalesfsp @waggledans
30 changes: 30 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Go

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: "Check out code into the Go module directory"
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15

- name: Setup golangci-lint
uses: golangci/golangci-lint-action@v2.5.2
with:
version: v1.41.1

- name: Lint
run: /home/runner/golangci-lint-1.41.1-linux-amd64/golangci-lint run -v -c .golangci.yml

- name: Test
run: make test coverage
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2021 The customerror Authors. All rights reserved.
# Use of this source code is governed by a MIT
# license that can be found in the LICENSE file.

# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
.direnv

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool
*.out
coverage.*

# Generated bin
bin/
dist/

# Temporary bin
tmp/

# Dependencies (run: go mod vendor)
vendor/

# IDE project files
*.vscode/

# PyCharm files
.idea/

# OS files
.DS_Store

# Everything else
tags

# Generated env.{environment} files
.env.*

# UML
*.plum

# scratch repo to hack
/scratch
68 changes: 68 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2021 The customerror Authors. All rights reserved.
# Use of this source code is governed by a MIT
# license that can be found in the LICENSE file.

run:
timeout: 1m
skip-files:
- internal/*

linters:
enable-all: true
disable:
- wrapcheck
- testpackage
- gochecknoglobals
- exhaustivestruct
- paralleltest
- godox
- cyclop
- tagliatelle
- goerr113
fast: false

# Settings for specific linters
linters-settings:
funlen:
lines: 150
statements: 45

issues:
exclude-rules:
- path: cmd/
linters:
- gochecknoinits
- gomnd
- forbidigo
- lll

- path: example_test.go
linters:
- lll

- path: internal/http/
linters:
- unparam
- nlreturn

- path: _test\.go
linters:
- scopelint
- wsl
- nlreturn
- funlen
- dupl
- gocognit

- path: doc.go
linters:
- lll

- linters:
- lll
source: "json:"

- linters:
- gocritic
- godot
source: "//////"
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Roadmap

## [0.0.1] - 2021-09-24

### Checklist

- [x] CI Pipeline:
- [x] Lint
- [x] Tests
- [x] Documentation:
- [x] Package's documentation (`doc.go`)
- [x] Meaningful code comments, and symbol names (`const`, `var`, `func`)
- [x] `GoDoc` server tested
- [x] `README.md`
- [x] `LICENSE`
- [x] Files has LICENSE in the header
- [x] Useful `CHANGELOG.md`
- [x] Clear `CONTRIBUTION.md`
- Automation:
- [x] `Makefile`
- Testing:
- [x] Coverage 80%+
- [x] Unit test
- [x] Real testing
- Examples:
- [x] Example's test file

### Added

- [x] Ability to create custom errors
- [x] Ability to create custom errors with code
- [x] Ability to create custom errors with status code
- [x] Ability to create custom errors with message
- [x] Ability to create custom errors wrapping an error
- [x] Ability to create static (pre-created) custom errors
- [x] Ability to create dynamic (in-line) custom errors
- [x] Ability to print a custom error with a dynamic, and custom message
11 changes: 11 additions & 0 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The following guidelines aim to point to a direction that should drive the codebase to increased quality.

- Each package should have a `doc.go` file.
- Think before you make changes, design, then code. Design patterns, and well-established techniques are your friend. They allow to reduce code duplication and complexity and increase reusability and performance.
- Documentation is essential! Relevant comments should be added focusing on the **why**, not in the **what**. _Pay attention to the punctuation and casing patterns_
- Pay attention to how the code is vertically spaced and positioned, also sorted (always ascending) for example, the content of a struct, `vars` and `const` declarations, and etc.
- If you use VSCode IDE, the Go extension is installed, **_and properly setup_**, it should obey the configuration file ([.golangci.yml](.golangci.yml)) for the linter (`golangci`) and show problems the right way, otherwise, just run `$ make lint`. The same thing applies to test. If you open a test file (`*_test.go`), modify and save it, it should automatically run tests and shows coverage; otherwise, just run `$ make test`
- Always run ` $ make coverage lint` before you commit your code; it will save you time!
- If you spotted a problem or something that needs to be modified/improved, do that right way; otherwise, that with `// TODO:`
- Update the [`CHANGELOG.md`](CHANGELOG.md) but not copying your commits messages - that's not its purpose. Use that to plan changes too.
- Don't write tests that test someone else package/APIs, for example, for a function that purely calls some Kubernetes API - they got that already tested, and covered in their own packages. If not, try to change the package you are using for something better. Focus on testing **business logic** and its desired outcomes.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Fernando Silva, Sauce Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2021 The customerror Authors. All rights reserved.
# Use of this source code is governed by a MIT
# license that can be found in the LICENSE file.

HAS_GOLANGCI := $(shell command -v golangci-lint;)
HAS_GODOC := $(shell command -v godoc;)

lint:
ifndef HAS_GOLANGCI
$(error You must install github.com/golangci/golangci-lint)
endif
@golangci-lint run -v -c .golangci.yml && echo "Lint OK"

test:
@go test -timeout 30s -short -v -race -cover -coverprofile=coverage.out ./...

coverage:
@go tool cover -func=coverage.out

doc:
ifndef HAS_GODOC
$(error You must install godoc, run "go get golang.org/x/tools/cmd/godoc")
endif
@echo "Open localhost:6060/pkg/github.com/saucelabs/sypl/ in your browser\n"
@godoc -http :6060

ci: lint test coverage

.PHONY: lint test coverage ci
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# customerror

`customerror` provides the base block to create custom errors. It also provides built-in custom errors covering some common cases. A Custom Error provides context - a `Message` to an optionally wrapped `Err`. Additionally a `Code` - for example "E1010", and `StatusCode` can be provided. Both static (pre-created), and dynamic (in-line) errors can be easily created. `Code` helps a company build a catalog of errors, which helps, and improves customer service.

## Install

`$ go get github.com/saucelabs/customerror@vX.Y.Z`

## Usage

See [`example_test.go`](example_test.go), and [`customerror_test.go`](customerror_test.go) file.

## Documentation

Run `$ make doc` or check out [online](https://pkg.go.dev/github.com/saucelabs/customerror).

## Development

Check out [CONTRIBUTION](CONTRIBUTION.md).

### Release

1. Update [CHANGELOG](CHANGELOG.md) accordingly.
2. Once changes from MR are merged.
3. Tag and release.

## Roadmap

Check out [CHANGELOG](CHANGELOG.md).
47 changes: 47 additions & 0 deletions builtin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2021 The customerror Authors. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package customerror

import (
"fmt"
"net/http"
)

//////
// Built-in.
//////

// NewFailedToError is the building block for errors usually thrown when some
// action failed, e.g: "Failed to create host". Default status code is `500`.
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewFailedToError(message string, code string, err error) error {
return New(fmt.Sprintf("failed to %s", message), code, http.StatusInternalServerError, err)
}

// NewInvalidError is the building block for errors usually thrown when
// something fail validation, e.g: "Invalid port". Default status code is `400`.
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewInvalidError(message string, code string, err error) error {
return New(fmt.Sprintf("invalid %s", message), code, http.StatusBadRequest, err)
}

// NewMissingError is the building block for errors usually thrown when required
// information is missing, e.g: "Missing host". Default status code is `400`.
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewMissingError(message string, code string, err error) error {
return New(fmt.Sprintf("missing %s", message), code, http.StatusBadRequest, err)
}

// NewRequiredError is the building block for errors usually thrown when
// required information is missing, e.g: "Port is required". Default status code
// is `400`.
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewRequiredError(message string, code string, err error) error {
return New(fmt.Sprintf("%s required", message), code, http.StatusBadRequest, err)
}
Loading

0 comments on commit 6e6370d

Please sign in to comment.