Giter VIP home page Giter VIP logo

sender's Introduction

HTTP JSON Sender

Sender is a GO library for create a HTTP JSON response using http.ResponseWriter as writer output.

Features

  • Send a JSON response error with a simple description error.
  • Send a JSON response error with a custom field error.
  • Send a JSON response error with a list of custom fields errors.
  • Send a JSON response object.

Installation

Standard go get:

$ go get github.com/mig-elgt/sender

Usage & Example

Handle to send a simple error description

func WithErrorHandle(w http.ResponseWriter, r *http.Request) {
	sender.
	   NewJSON(w, http.StatusInternalServerError).
	   WithError(codes.Internal, "Something went wrong..").
	   Send()
}
// Output
// {
//     "error": {
//         "status": 500,
//         "error": "INTERNAL",
//         "description": "Something went wrong..."
//     }
// }

Handle to send a simple error with a field value.

func WithFieldErrorHandle(w http.ResponseWriter, r *http.Request) {
	sender.
	   NewJSON(w, http.StatusBadRequest).
	   WithFieldError(
	      codes.InvalidArgument,
		  "user_id",
		  "User ID is required",
	   ).
	   Send()
}

// Output
// {
//     "error": {
//         "status": 400,
//         "error": "INVALID_ARGUMENT",
//         "description": "One or more fields raised validation errors.",
//         "fields": {
//             "user_id": "User ID is required"
//         }
//     }
// }

Handle to send an error with a set of fields inputs.

func WithFieldsErrorHandle(w http.ResponseWriter, r *http.Request) {
	sender.
	  NewJSON(w, http.StatusBadRequest).
	  WithFieldsError(
		codes.InvalidArgument,
			map[string]string{
				"user_id": "User ID is required",
				"email":   "Email has invalid format",
			},
	  ).
	  Send()
}

// Output
// {
//     "error": {
//         "status": 400,
//         "error": "INVALID_ARGUMENT",
//         "description": "One or more fields raised validation errors.",
//         "fields": {
//             "email": "Email has invalid format",
//             "user_id": "User ID is required"
//         }
//     }
// }

Handle to send a JSON object without errors.

func Handle(w http.ResponseWriter, r *http.Request) {
   type response struct {
     Status int `json:"status"`
	 ID int     `json:"id"` 
   }
   sender.
      NewJSON(w, http.StatusOk).
	  Send(&response{
	     Status: 200,
	     ID: 100,
	  })
}

// Output
// {
//  	"status": 200,
//  	"id": 100,
// }

sender's People

Contributors

mig-elgt avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.