Skip to content

Commit

Permalink
feat: create httperror library (#1)
Browse files Browse the repository at this point in the history
* feat: create httperror library

* fix: set go 1.21
  • Loading branch information
ermos authored Nov 18, 2023
1 parent 8c2b2b1 commit 7871c32
Show file tree
Hide file tree
Showing 20 changed files with 864 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
57 changes: 57 additions & 0 deletions .github/workflows/go-codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: "Go / CodeQL"

on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
- cron: '30 1 * * 0'

jobs:
analyse:
# CodeQL runs on ubuntu-latest, windows-latest, and macos-latest
runs-on: ubuntu-latest

permissions:
# required for all workflows
security-events: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
# Override language selection by uncommenting this and choosing your languages
with:
languages: go

# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below).
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# ✏️ If the Autobuild fails above, remove it and uncomment the following
# three lines and modify them (or add more) to build your code if your
# project uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
37 changes: 37 additions & 0 deletions .github/workflows/go-govuln-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "Go / Vuln Check"

on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
- cron: '30 1 * * 0'

jobs:
govulncheck:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
check-latest: true
- name: Fetch Repository
uses: actions/checkout@v3
- name: Install Govulncheck
run: |
export GO111MODULE=on
export PATH=${PATH}:`go env GOPATH`/bin
go install golang.org/x/vuln/cmd/govulncheck@latest
- name: Run Govulncheck
run: "`go env GOPATH`/bin/govulncheck ./..."
56 changes: 56 additions & 0 deletions .github/workflows/go-lint-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: "Go / Lint Check"

on:
push:
branches:
- master
- main
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.54

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
# args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true, then all caching functionality will be completely disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
18 changes: 18 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Go / Test"
on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.20.1
- name: Run coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

# Jetbrains
.idea/
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
lint:
@go fmt . && golangci-lint run

test:
@go test

test/coverage:
@go test -cover

test/html:
@go test -coverprofile=coverage.out
@go tool cover -html=coverage.out

generate/examples:
@cd ./examples/add-host && go run .
@cd ./examples/edit-host && go run .
@cd ./examples/remove-host && go run .
@cd ./examples/add-raw && go run .
@cd ./examples/clear-hosts-file && go run .
@cd ./examples/new-hosts-file && go run .
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
# httperror
Go package that provides features to handle errors with http services
> Go package that provides features to handle errors with http services
[![Go Reference](https://pkg.go.dev/badge/github.com/ermos/httperror.svg)](https://pkg.go.dev/github.com/ermos/httperror)
[![Latest tag](https://img.shields.io/github/v/tag/ermos/httperror?label=latest)](https://github.com/ermos/httperror/tags)
[![Go Report Card](https://goreportcard.com/badge/github.com/ermos/httperror)](https://goreportcard.com/report/github.com/ermos/httperror)
[![Maintainability](https://api.codeclimate.com/v1/badges/4d4e0548b083618eba7d/maintainability)](https://codeclimate.com/github/ermos/httperror/maintainability)
[![codecov](https://codecov.io/github/ermos/httperror/branch/main/graph/badge.svg?token=52FCLZVA0B)](https://codecov.io/github/ermos/httperror)

## 🛠️ Installation

To use `httperror` in your Go project, you need to have Go installed on your system.
Once you have Go installed, you can use the following command to install the package:

```go
go get -u github.com/ermos/httperror
```

## 🤝 Contributing

Contributions to `httperror` are always welcome!
If you find a bug or have a feature request, please open an issue on GitHub.
If you want to contribute code, please fork the repository and submit a pull request.
28 changes: 28 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package httperror

import (
"context"
)

type Error struct {
message string
code string
status int
ctx context.Context
}

func (e Error) Error() string {
return e.message
}

func (e Error) Code() string {
return e.code
}

func (e Error) Status() int {
return e.status
}

func (e Error) Context() context.Context {
return e.ctx
}
50 changes: 50 additions & 0 deletions error_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package httperror

import (
"net/http"
)

// NewBadRequest returns a new error with status code 400.
func NewBadRequest(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusBadRequest, options...)
}

// NewInternalServerError returns a new error with status code 500.
func NewInternalServerError(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusInternalServerError, options...)
}

// NewNotFound returns a new error with status code 404.
func NewNotFound(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusNotFound, options...)
}

// NewUnauthorized returns a new error with status code 401.
func NewUnauthorized(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusUnauthorized, options...)
}

// NewForbidden returns a new error with status code 403.
func NewForbidden(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusForbidden, options...)
}

// NewConflict returns a new error with status code 409.
func NewConflict(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusConflict, options...)
}

// NewNotImplemented returns a new error with status code 501.
func NewNotImplemented(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusNotImplemented, options...)
}

// NewMethodNotAllowed returns a new error with status code 405.
func NewMethodNotAllowed(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusMethodNotAllowed, options...)
}

// NewUnprocessableEntity returns a new error with status code 422.
func NewUnprocessableEntity(err interface{}, options ...ErrorOption) error {
return NewError(err, http.StatusUnprocessableEntity, options...)
}
Loading

0 comments on commit 7871c32

Please sign in to comment.