Skip to content

junkd0g/neji

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report Card License: MIT GoDoc

neji

neji is a lightweight Go package for generating structured JSON error responses and handling parameter validation errors. It provides utility functions to standardize error handling in API applications.

Features

  • 🔧 Structured JSON Error Responses: Generate consistent error responses in two formats
  • Parameter Validation: Standardized missing parameter error messages
  • 🔗 Error Wrapping: Preserve original errors while adding context
  • 🧪 Well Tested: Comprehensive test coverage with edge cases
  • 📦 Zero Dependencies: Only standard library (except for tests)

Installing

go get -u github.com/junkd0g/neji

✅ Running Tests

go test ./...

🚀 Usage

Basic Error Response

package main

import (
	"errors"
	"net/http"

	"github.com/gorilla/mux"
	nerror "github.com/junkd0g/neji"
)

func HelloWorld(w http.ResponseWriter, r *http.Request) {
	err := errors.New("This is the best error message ever")
	errorResponse, _ := nerror.SimpleErrorResponseWithStatus(500, err)

	w.Header().Set("Content-Type", "application/json")
	w.Write(errorResponse)
}

func main() {
	router := mux.NewRouter().StrictSlash(true)
	router.HandleFunc("/", HelloWorld)
	http.ListenAndServe(":8076", router)
}

API Reference

SimpleErrorResponseWithStatus

nerror.SimpleErrorResponseWithStatus(500, err)

Returns:

{
	"message": "Your json is wrong or something",
	"status": 500
}

SimpleErrorResponseWithCodeV2

nerror.SimpleErrorResponseWithCodeV2(500, err)

Returns:

{
	"error": {
		"status": 500,
		"message": "Your json is wrong or something"
	}
}

Parameter Validation

// Generate standardized parameter error
err := nerror.ErrInvalidParameter("user_id")
// Returns: "missing parameter user_id"

Error Wrapping

// Wrap errors with additional context
originalErr := errors.New("connection timeout")
wrappedErr := nerror.WrapError(originalErr, "failed to fetch data")
// Returns: "failed to fetch data: connection timeout"

📝 License

This project is licensed under the MIT License. See the LICENSE file for details.

Authors

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages