Skip to content

Commit

Permalink
Updated reply objects to have necessary fields and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon Silcott committed Sep 9, 2021
1 parent bf721d0 commit 0fdaccf
Showing 1 changed file with 109 additions and 20 deletions.
129 changes: 109 additions & 20 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,135 @@

package reply

import "net/http"
import (
"net/http"
"strconv"
)

// ErrorManifestItem holds the message and status code for an error response
type ErrorManifestItem struct {

// Message holds the text returned in the response's `status.message` path
// Title holds the text summary returned in the response's error response
//
// NOTE:
//
// - The most effective messages are short, sweet and easy to consume
// - The most effective title are short, sweet and easy to consume
//
// - This message will be seen by the consuming client, be mindful of
// the amount of information you divulge
Message string
Title string

// Detail holds a more descriptive brief returned in the response's error response
//
// NOTE:
//
// - Detail will give a deeper level of context, while being mindful of length
//
// - Like the title message will be seen by the consuming client, be mindful of
// the amount of information you divulge
Detail string

// StatusCode holds the HTTP status code that best relates to the response.
// For more information on status codes, https://httpstatuses.com/.
StatusCode int

// About holds the a URL that gives further insight into the error
About string

// Code holds the internal application error code, if appicable, thst is used to
// help debuggers better identify error
Code string

// Meta contains additional meta-information about the that can be shared to
// consumer
Meta interface{}
}

// ErrorManifest holds error reference (string) with its corresponding
// manifest item (message & status code) which it returned in the response
type ErrorManifest map[string]ErrorManifestItem

// TransferObjectStatus holds attributes often used to give additional
// context in responses
type TransferObjectStatus struct {
Errors []Error `json:"errors,omitempty"`
Message string `json:"message,omitempty"`
// Error holds attributes often used to give additional
// context when unexpected behaviour occurs
type Error struct {

// Title a short summary of the problem
Title string `json:"title,omitempty"`

// Detail a description of the error
Detail string `json:"detail,omitempty"`

// About holds the link that gives further insight into the error
About string `json:"about,omitempty"`

// Status the HTTP status associated with error
Status string `json:"status,omitempty"`

// Code internal error code used to reference error
Code string `json:"code,omitempty"`

// Meta contains additional meta-information about the error
Meta interface{} `json:"meta,omitempty"`
}

// SetMessage adds message to transfer object status
func (s *TransferObjectStatus) SetMessage(message string) {
s.Message = message
// SetTitle adds title to error
func (e *Error) SetTitle(title string) {
e.Title = title
}

// Error holds the associated code and detail for errors passed
type Error struct {
Code string `json:"code"`
Details string `json:"details"`
// GetTitle returns error's title
func (e *Error) GetTitle() string {
return e.Title
}

// SetDetail adds detail to error
func (e *Error) SetDetail(detail string) {
e.Detail = detail
}

// GetDetail return error's detail
func (e *Error) GetDetail() string {
return e.Detail
}

// SetAbout adds about to error
func (e *Error) SetAbout(about string) {
e.About = about
}

// GetAbout return error's about
func (e *Error) GetAbout() string {
return e.About
}

// SetStatusCode converts and add http status code to error
func (e *Error) SetStatusCode(status int) {
e.Status = strconv.Itoa(status)
}

// GetStatusCode returns error's HTTP status code
func (e *Error) GetStatusCode() string {
return e.Status
}

// SetCode adds internal code to error
func (e *Error) SetCode(code string) {
e.Code = code
}

// GetCode returns error's internal code
func (e *Error) GetCode() string {
return e.Code
}

// SetMeta adds meta property to error
func (e *Error) SetMeta(meta interface{}) {
e.Meta = meta
}

// GetMeta returns error's meta property
func (e *Error) GetMeta() interface{} {
return e.Meta
}

// defaultReplyTransferObject handles structing response for client
Expand All @@ -52,7 +141,7 @@ type defaultReplyTransferObject struct {
HTTPWriter http.ResponseWriter `json:"-"`
Headers map[string]string `json:"-"`
StatusCode int `json:"-"`
Status *TransferObjectStatus `json:"status,omitempty"`
Errors []TransferObjectError `json:"errors,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
Data interface{} `json:"data,omitempty"`
AccessToken string `json:"access_token,omitempty"`
Expand Down Expand Up @@ -111,7 +200,7 @@ func (t *defaultReplyTransferObject) RefreshTransferObject() TransferObject {
return &defaultReplyTransferObject{}
}

// SetStatus assigns the passed transfer object status to the transfer object
func (t *defaultReplyTransferObject) SetStatus(transferObjectStatus *TransferObjectStatus) {
t.Status = transferObjectStatus
// SetErrors assigns the passed transfer object errors to the transfer object
func (t *defaultReplyTransferObject) SetErrors(transferObjectErrors []TransferObjectError) {
t.Errors = transferObjectErrors
}

0 comments on commit 0fdaccf

Please sign in to comment.