Skip to content

Commit

Permalink
- Renamed ProblemDetail Struct to Problem
Browse files Browse the repository at this point in the history
- Updated date on CHANGELOG.md
  • Loading branch information
shelleeboo committed Oct 15, 2024
1 parent 8be0c2f commit d352bf4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file.
We follow the [Semantic Versioning 2.0.0](http://semver.org/) format.


## 1.0.0-2024-10-09
## 1.0.0-2024-10-15

Initial Release

Expand Down
68 changes: 34 additions & 34 deletions problemdetails/problemdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const (
MIMETypeJSON = "application/problem+json"
)

// ProblemDetails represents a HTTP Problem Details structure from https://www.rfc-editor.org/rfc/rfc9457.html
type ProblemDetails struct {
// Problem represents a HTTP Problem Details structure from https://www.rfc-editor.org/rfc/rfc9457.html
type Problem struct {
Type string `json:"type"`
Status int `json:"status"`
Title string `json:"title,omitempty"`
Expand All @@ -21,92 +21,92 @@ type ProblemDetails struct {
Extensions map[string]any `json:"extensions,omitempty"`
}

// WithTitle sets a custom Title value on the ProblemDetails object. The ProblemDetails pointer is
// WithTitle sets a custom Title value on the Problem object. The Problem pointer is
// returned only for developer convenience.
func (p *ProblemDetails) WithTitle(title string) *ProblemDetails {
func (p *Problem) WithTitle(title string) *Problem {
p.Title = title
return p
}

// WithDetail sets a custom Detail value on the ProblemDetails object. The ProblemDetails pointer is
// WithDetail sets a custom Detail value on the Problem object. The Problem pointer is
// returned only for developer convenience.
func (p *ProblemDetails) WithDetail(detail string) *ProblemDetails {
func (p *Problem) WithDetail(detail string) *Problem {
p.Detail = detail
return p
}

// WithExtension adds a custom Extension value on the ProblemDetails object. The ProblemDetails pointer is
// WithExtension adds a custom Extension value on the Problem object. The Problem pointer is
// returned only for developer convenience.
func (p *ProblemDetails) WithExtension(name string, value any) *ProblemDetails {
func (p *Problem) WithExtension(name string, value any) *Problem {
if p.Extensions == nil {
p.Extensions = make(map[string]any)
}
p.Extensions[name] = value
return p //&p recreates pointer?
}

// New creates a new ProblemDetails instance. Do not use this function unless you are creating a
// custom ProblemDetails instance. Prefer using the functions that create standard ProblemDetails instances.
func New(typeUri string, status int) *ProblemDetails {
return &ProblemDetails{
// New creates a new Problem instance. Do not use this function unless you are creating a
// custom Problem instance. Prefer using the functions that create standard Problem instances.
func New(typeUri string, status int) *Problem {
return &Problem{
Type: typeUri,
Status: status,
Instance: uuidUrnPrefix + uuid.NewString(),
Created: time.Now().UTC().Format(time.RFC3339Nano),
}
}

// NewBadRequest creates a new ProblemDetails instance that represents an HTTP 400 Bad Request.
func NewBadRequest() *ProblemDetails {
// NewBadRequest creates a new Problem instance that represents an HTTP 400 Bad Request.
func NewBadRequest() *Problem {
return New(badRequest.urn, badRequest.code).WithTitle(badRequest.title)
}

// NewUnauthorized creates a new ProblemDetails instance that represents an HTTP 401 Unauthorized.
func NewUnauthorized() *ProblemDetails {
// NewUnauthorized creates a new Problem instance that represents an HTTP 401 Unauthorized.
func NewUnauthorized() *Problem {
return New(unauthorized.urn, unauthorized.code).WithTitle(unauthorized.title)
}

// NewForbidden creates a new ProblemDetails instance that represents an HTTP 403 Forbidden.
func NewForbidden() *ProblemDetails {
// NewForbidden creates a new Problem instance that represents an HTTP 403 Forbidden.
func NewForbidden() *Problem {
return New(forbidden.urn, forbidden.code).WithTitle(forbidden.title)
}

// NewNotFound creates a new ProblemDetails instance that represents an HTTP 404 Not Found.
func NewNotFound() *ProblemDetails {
// NewNotFound creates a new Problem instance that represents an HTTP 404 Not Found.
func NewNotFound() *Problem {
return New(notFound.urn, notFound.code).WithTitle(notFound.title)
}

// NewMethodNotAllowed creates a new ProblemDetails instance that represents an HTTP 405 Method Not Allowed.
func NewMethodNotAllowed() *ProblemDetails {
// NewMethodNotAllowed creates a new Problem instance that represents an HTTP 405 Method Not Allowed.
func NewMethodNotAllowed() *Problem {
return New(methodNotAllowed.urn, methodNotAllowed.code).WithTitle(methodNotAllowed.title)
}

// NewConflict creates a new ProblemDetails instance that represents an HTTP 409 Conflict.
func NewConflict() *ProblemDetails {
// NewConflict creates a new Problem instance that represents an HTTP 409 Conflict.
func NewConflict() *Problem {
return New(conflict.urn, conflict.code).WithTitle(conflict.title)
}

// NewTooManyRequests creates a new ProblemDetails instance that represents an HTTP 429 Too Many Requests.
func NewTooManyRequests() *ProblemDetails {
// NewTooManyRequests creates a new Problem instance that represents an HTTP 429 Too Many Requests.
func NewTooManyRequests() *Problem {
return New(tooManyRequests.urn, tooManyRequests.code).WithTitle(tooManyRequests.title)
}

// NewInternalServerError creates a new ProblemDetails instance that represents an HTTP 500 Internal Server Error.
func NewInternalServerError() *ProblemDetails {
// NewInternalServerError creates a new Problem instance that represents an HTTP 500 Internal Server Error.
func NewInternalServerError() *Problem {
return New(internalServerError.urn, internalServerError.code).WithTitle(internalServerError.title)
}

// NewBadGateway creates a new ProblemDetails instance that represents an HTTP 502 Bad Gateway.
func NewBadGateway() *ProblemDetails {
// NewBadGateway creates a new Problem instance that represents an HTTP 502 Bad Gateway.
func NewBadGateway() *Problem {
return New(badGateway.urn, badGateway.code).WithTitle(badGateway.title)
}

// NewServiceUnavailable creates a new ProblemDetails instance that represents an HTTP 503 Service Unavailable.
func NewServiceUnavailable() *ProblemDetails {
// NewServiceUnavailable creates a new Problem instance that represents an HTTP 503 Service Unavailable.
func NewServiceUnavailable() *Problem {
return New(serviceUnavailable.urn, serviceUnavailable.code).WithTitle(serviceUnavailable.title)
}

// NewGatewayTimeout creates a new ProblemDetails instance that represents an HTTP 504 Gateway Timeout.
func NewGatewayTimeout() *ProblemDetails {
// NewGatewayTimeout creates a new Problem instance that represents an HTTP 504 Gateway Timeout.
func NewGatewayTimeout() *Problem {
return New(gatewayTimeout.urn, gatewayTimeout.code).WithTitle(gatewayTimeout.title)
}
2 changes: 1 addition & 1 deletion problemdetails/problemdetails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestNewWithExtension(t *testing.T) {
doCommonAssertions(t, problemDetails)
}

func doCommonAssertions(t *testing.T, problemDetails *ProblemDetails) {
func doCommonAssertions(t *testing.T, problemDetails *Problem) {
assert.Equal(t, true, strings.Contains(problemDetails.Instance, uuidUrnPrefix))

var createdTime time.Time
Expand Down

0 comments on commit d352bf4

Please sign in to comment.