Skip to content

Commit

Permalink
refactor: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
le0m committed Aug 5, 2024
1 parent eee8ccb commit 82b630d
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 58 deletions.
8 changes: 4 additions & 4 deletions lambda/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.5

require (
github.com/aws/aws-lambda-go v1.47.0
github.com/chialab/print2pdf-go/print2pdf v0.0.0-20240801003124-bff0528902dd
github.com/chialab/print2pdf-go/print2pdf v0.1.6
)

require (
Expand All @@ -21,13 +21,13 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/chromedp/cdproto v0.0.0-20240721024200-dac8efcb39ce // indirect
github.com/chromedp/chromedp v0.9.5 // indirect
github.com/chromedp/cdproto v0.0.0-20240801214329-3f85d328b335 // indirect
github.com/chromedp/chromedp v0.10.0 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
Expand Down
12 changes: 12 additions & 0 deletions lambda/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15 h1:246A4lSTXWJw/
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15/go.mod h1:haVfg3761/WF7YPuJOER2MP0k4UAXyHaLclKXB6usDg=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2 h1:sZXIzO38GZOU+O0C+INqbH7C2yALwfMWpd64tONS/NE=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2/go.mod h1:Lcxzg5rojyVPU/0eFwLtcyTaek/6Mtic5B1gJo7e/zE=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3 h1:hT8ZAZRIfqBqHbzKTII+CIiY8G2oC9OpLedkZ51DWl8=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3/go.mod h1:Lcxzg5rojyVPU/0eFwLtcyTaek/6Mtic5B1gJo7e/zE=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE=
Expand All @@ -38,11 +40,21 @@ github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/chialab/print2pdf-go/print2pdf v0.0.0-20240801003124-bff0528902dd h1:86yHQJAgswmXKf/ejWdYPy0h5WG4w0KheDEKW15i1WA=
github.com/chialab/print2pdf-go/print2pdf v0.0.0-20240801003124-bff0528902dd/go.mod h1:r70w2eB4GpO/Zn3Sw+pzWSo7kJn206wNHA35hcx6V0I=
github.com/chialab/print2pdf-go/print2pdf v0.1.4 h1:SMsbEABbTCDZo2v3VidbVok1tfbOyLewkE12eepxT8c=
github.com/chialab/print2pdf-go/print2pdf v0.1.4/go.mod h1:zBUOgJd/WGICjtzth3XGkkpBm2wV2VezmJtw72Hmnv8=
github.com/chialab/print2pdf-go/print2pdf v0.1.5 h1:p+NdE4z7jBKXvzpwyS0Le5ipDRYX9X9QpLUtK9zEzmc=
github.com/chialab/print2pdf-go/print2pdf v0.1.5/go.mod h1:zBUOgJd/WGICjtzth3XGkkpBm2wV2VezmJtw72Hmnv8=
github.com/chialab/print2pdf-go/print2pdf v0.1.6 h1:9VPqq1iLSdYi1w56q6R//r6GMdoGTdRmCbZP2TTr+Tg=
github.com/chialab/print2pdf-go/print2pdf v0.1.6/go.mod h1:zBUOgJd/WGICjtzth3XGkkpBm2wV2VezmJtw72Hmnv8=
github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/cdproto v0.0.0-20240721024200-dac8efcb39ce h1:pvzUsAunw3R7swXkLT6vqv81Awhnds43mbZHAzhn2pQ=
github.com/chromedp/cdproto v0.0.0-20240721024200-dac8efcb39ce/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/cdproto v0.0.0-20240801214329-3f85d328b335 h1:bATMoZLH2QGct1kzDxfmeBUQI/QhQvB0mBrOTct+YlQ=
github.com/chromedp/cdproto v0.0.0-20240801214329-3f85d328b335/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/chromedp v0.9.5 h1:viASzruPJOiThk7c5bueOUY91jGLJVximoEMGoH93rg=
github.com/chromedp/chromedp v0.9.5/go.mod h1:D4I2qONslauw/C7INoCir1BJkSwBYMyZgx8X276z3+Y=
github.com/chromedp/chromedp v0.10.0 h1:bRclRYVpMm/UVD76+1HcRW9eV3l58rFfy7AdBvKab1E=
github.com/chromedp/chromedp v0.10.0/go.mod h1:ei/1ncZIqXX1YnAYDkxhD4gzBgavMEUu7JCKvztdomE=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
78 changes: 57 additions & 21 deletions lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"os"
"slices"
"strings"
Expand All @@ -12,57 +13,92 @@ import (
"github.com/chialab/print2pdf-go/print2pdf"
)

var BucketName = os.Getenv("BUCKET")
type ErrorResponse struct {
Message string `json:"message"`
}

var CorsAllowedHosts = os.Getenv("CORS_ALLOWED_HOSTS")

func main() {
lambda.Start(handler)
}

// Handle a request.
func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
responseHeaders := map[string]string{"Content-Type": "application/json"}
headers := map[string]string{"Content-Type": "application/json"}
origin, ok := event.Headers["Origin"]
if !ok {
origin = event.Headers["origin"]
}
if CorsAllowedHosts != "" && origin != "" {
if CorsAllowedHosts == "*" {
responseHeaders["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Origin"] = "*"
} else {
allowedHosts := strings.Split(CorsAllowedHosts, ",")
if slices.Contains(allowedHosts, origin) {
responseHeaders["Access-Control-Allow-Origin"] = origin
hosts := strings.Split(CorsAllowedHosts, ",")
if slices.Contains(hosts, origin) {
headers["Access-Control-Allow-Origin"] = origin
}
}
}

var requestData print2pdf.RequestData
err := json.Unmarshal([]byte(event.Body), &requestData)
var data print2pdf.RequestData
err := json.Unmarshal([]byte(event.Body), &data)
if err != nil {
return events.APIGatewayProxyResponse{}, err
fmt.Fprintf(os.Stderr, "error decoding JSON: %s\noriginal request body: %s\n", err, event.Body)

return JsonError("internal server error", 500), nil
}

var buf []byte
err = print2pdf.GetPDFBuffer(ctx, requestData, &buf)
if err != nil {
return events.APIGatewayProxyResponse{}, err
err = print2pdf.GetPDFBuffer(ctx, data, &buf)
if ve, ok := err.(print2pdf.ValidationError); ok {
fmt.Fprintf(os.Stderr, "request validation error: %s\n", ve)

return JsonError(ve.Error(), 400), nil
} else if err != nil {
fmt.Fprintf(os.Stderr, "error getting PDF buffer: %s\n", err)

return JsonError("internal server error", 500), nil
}

url, err := print2pdf.UploadFile(ctx, BucketName, requestData.FileName, &buf)
url, err := print2pdf.UploadFile(ctx, data.FileName, &buf)
if err != nil {
return events.APIGatewayProxyResponse{}, err
fmt.Fprintf(os.Stderr, "error uploading file: %s\n", err)

return JsonError("internal server error", 500), nil
}

responseData := print2pdf.ResponseData{Url: url}
responseJson, err := json.Marshal(responseData)
body, err := json.Marshal(print2pdf.ResponseData{Url: url})
if err != nil {
return events.APIGatewayProxyResponse{}, err
fmt.Fprintf(os.Stderr, "error encoding response to JSON: %s\n", err)

return JsonError("internal server error", 500), nil
}

return events.APIGatewayProxyResponse{
StatusCode: 200,
Body: string(responseJson),
Headers: responseHeaders,
Body: string(body),
Headers: headers,
}, nil
}

func main() {
lambda.Start(handler)
// Prepare an HTTP error response.
func JsonError(message string, code int) events.APIGatewayProxyResponse {
ct := "application/json"
body, err := json.Marshal(ErrorResponse{message})
if err != nil {
fmt.Fprintf(os.Stderr, "error encoding error message to JSON: %s\noriginal error: %s\n", err, message)
body = []byte("internal server error")
code = 500
ct = "text/plain"
}

return events.APIGatewayProxyResponse{
StatusCode: code,
Body: string(body),
Headers: map[string]string{
"Content-Type": ct,
"X-Content-Type-Options": "nosniff",
},
}
}
8 changes: 4 additions & 4 deletions plain/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/chialab/print2pdf-go/plain

go 1.22.5

require github.com/chialab/print2pdf-go/print2pdf v0.0.0-20240801004620-b2cc33f39506
require github.com/chialab/print2pdf-go/print2pdf v0.1.6

require (
github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect
Expand All @@ -18,13 +18,13 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/chromedp/cdproto v0.0.0-20240721024200-dac8efcb39ce // indirect
github.com/chromedp/chromedp v0.9.5 // indirect
github.com/chromedp/cdproto v0.0.0-20240801214329-3f85d328b335 // indirect
github.com/chromedp/chromedp v0.10.0 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
Expand Down
12 changes: 12 additions & 0 deletions plain/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15 h1:246A4lSTXWJw/
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15/go.mod h1:haVfg3761/WF7YPuJOER2MP0k4UAXyHaLclKXB6usDg=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2 h1:sZXIzO38GZOU+O0C+INqbH7C2yALwfMWpd64tONS/NE=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2/go.mod h1:Lcxzg5rojyVPU/0eFwLtcyTaek/6Mtic5B1gJo7e/zE=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3 h1:hT8ZAZRIfqBqHbzKTII+CIiY8G2oC9OpLedkZ51DWl8=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3/go.mod h1:Lcxzg5rojyVPU/0eFwLtcyTaek/6Mtic5B1gJo7e/zE=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE=
Expand All @@ -36,11 +38,21 @@ github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/chialab/print2pdf-go/print2pdf v0.0.0-20240801004620-b2cc33f39506 h1:HPYUc6DVZX7Ja736075Fqs4LZU/EdcRCAu7hevsFPF0=
github.com/chialab/print2pdf-go/print2pdf v0.0.0-20240801004620-b2cc33f39506/go.mod h1:r70w2eB4GpO/Zn3Sw+pzWSo7kJn206wNHA35hcx6V0I=
github.com/chialab/print2pdf-go/print2pdf v0.1.4 h1:SMsbEABbTCDZo2v3VidbVok1tfbOyLewkE12eepxT8c=
github.com/chialab/print2pdf-go/print2pdf v0.1.4/go.mod h1:zBUOgJd/WGICjtzth3XGkkpBm2wV2VezmJtw72Hmnv8=
github.com/chialab/print2pdf-go/print2pdf v0.1.5 h1:p+NdE4z7jBKXvzpwyS0Le5ipDRYX9X9QpLUtK9zEzmc=
github.com/chialab/print2pdf-go/print2pdf v0.1.5/go.mod h1:zBUOgJd/WGICjtzth3XGkkpBm2wV2VezmJtw72Hmnv8=
github.com/chialab/print2pdf-go/print2pdf v0.1.6 h1:9VPqq1iLSdYi1w56q6R//r6GMdoGTdRmCbZP2TTr+Tg=
github.com/chialab/print2pdf-go/print2pdf v0.1.6/go.mod h1:zBUOgJd/WGICjtzth3XGkkpBm2wV2VezmJtw72Hmnv8=
github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/cdproto v0.0.0-20240721024200-dac8efcb39ce h1:pvzUsAunw3R7swXkLT6vqv81Awhnds43mbZHAzhn2pQ=
github.com/chromedp/cdproto v0.0.0-20240721024200-dac8efcb39ce/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/cdproto v0.0.0-20240801214329-3f85d328b335 h1:bATMoZLH2QGct1kzDxfmeBUQI/QhQvB0mBrOTct+YlQ=
github.com/chromedp/cdproto v0.0.0-20240801214329-3f85d328b335/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/chromedp v0.9.5 h1:viASzruPJOiThk7c5bueOUY91jGLJVximoEMGoH93rg=
github.com/chromedp/chromedp v0.9.5/go.mod h1:D4I2qONslauw/C7INoCir1BJkSwBYMyZgx8X276z3+Y=
github.com/chromedp/chromedp v0.10.0 h1:bRclRYVpMm/UVD76+1HcRW9eV3l58rFfy7AdBvKab1E=
github.com/chromedp/chromedp v0.10.0/go.mod h1:ei/1ncZIqXX1YnAYDkxhD4gzBgavMEUu7JCKvztdomE=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
Expand Down
93 changes: 64 additions & 29 deletions plain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"slices"
Expand All @@ -12,13 +13,20 @@ import (
"github.com/chialab/print2pdf-go/print2pdf"
)

type ErrorResponse struct {
Message string `json:"message"`
}

var Port = os.Getenv("PORT")
var BucketName = os.Getenv("BUCKET")
var CorsAllowedHosts = os.Getenv("CORS_ALLOWED_HOSTS")

func main() {
if Port == "" {
Port = "3000"
}

http.HandleFunc("/print", printHandler)
fmt.Printf("server listening on port %s", Port)
fmt.Printf("server listening on port %s\n", Port)
err := http.ListenAndServe(":"+Port, nil)
if errors.Is(err, http.ErrServerClosed) {
fmt.Println("server closed")
Expand All @@ -30,16 +38,14 @@ func main() {

// Handle requests to "/print" endpoint.
func printHandler(w http.ResponseWriter, r *http.Request) {
go func() {
switch r.Method {
case "OPTIONS":
handlePrintOptions(w, r)
case "POST":
handlePrintPost(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}()
switch r.Method {
case "OPTIONS":
handlePrintOptions(w, r)
case "POST":
handlePrintPost(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}

// Handle OPTIONS requests to "/print" endpoint.
Expand All @@ -61,6 +67,7 @@ func handlePrintOptions(w http.ResponseWriter, r *http.Request) {

// Handle POST requests to "/print" endpoint.
func handlePrintPost(w http.ResponseWriter, r *http.Request) {
r.Context()
w.Header().Set("Content-Type", "application/json")

if CorsAllowedHosts == "" || CorsAllowedHosts == "*" {
Expand All @@ -73,49 +80,77 @@ func handlePrintPost(w http.ResponseWriter, r *http.Request) {
}
}

var requestData print2pdf.RequestData
err := json.NewDecoder(r.Body).Decode(&requestData)
body, err := io.ReadAll(r.Body)
if err != nil {
JsonError(w, "Error reading request data", http.StatusInternalServerError)
fmt.Fprintf(os.Stderr, "error reading request data: %s\n", err)
JsonError(w, "internal server error", http.StatusInternalServerError)

return
}

var data print2pdf.RequestData
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Fprintf(os.Stderr, "error decoding JSON: %s\noriginal request body: %s\n", err, string(body))
JsonError(w, "internal server error", http.StatusInternalServerError)

return
}

var buf []byte
err = print2pdf.GetPDFBuffer(r.Context(), requestData, &buf)
if e, ok := err.(print2pdf.ValidationError); ok {
JsonError(w, e.Error(), http.StatusBadRequest)
err = print2pdf.GetPDFBuffer(r.Context(), data, &buf)
if ve, ok := err.(print2pdf.ValidationError); ok {
fmt.Fprintf(os.Stderr, "request validation error: %s\n", ve)
JsonError(w, ve.Error(), http.StatusBadRequest)

return
} else if err != nil {
fmt.Fprintf(os.Stderr, "error getting PDF buffer: %s", err)
JsonError(w, "Internal server error", http.StatusInternalServerError)
fmt.Fprintf(os.Stderr, "error getting PDF buffer: %s\n", err)
JsonError(w, "internal server error", http.StatusInternalServerError)

return
}

url, err := print2pdf.UploadFile(r.Context(), BucketName, requestData.FileName, &buf)
url, err := print2pdf.UploadFile(r.Context(), data.FileName, &buf)
if err != nil {
fmt.Fprintf(os.Stderr, "error uploading file: %s", err)
JsonError(w, "Internal server error", http.StatusInternalServerError)
fmt.Fprintf(os.Stderr, "error uploading file: %s\n", err)
JsonError(w, "internal server error", http.StatusInternalServerError)

return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(print2pdf.ResponseData{Url: url})
}
body, err = json.Marshal(print2pdf.ResponseData{Url: url})
if err != nil {
fmt.Fprintf(os.Stderr, "error encoding response to JSON: %s\n", err)
JsonError(w, "internal server error", http.StatusInternalServerError)

type ErrorResponse struct {
Message string `json:"message"`
return
}

w.WriteHeader(http.StatusOK)
_, err = w.Write(body)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing response: %s\n", err)
}
}

// JsonError replies to the request with the specified error message and HTTP code.
// It does not otherwise end the request; the caller should ensure no further
// writes are done to w.
func JsonError(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json")
body, err := json.Marshal(ErrorResponse{message})
if err != nil {
fmt.Fprintf(os.Stderr, "error encoding error message to JSON: %s\noriginal error: %s\n", err, message)
body = []byte("internal server error")
code = http.StatusInternalServerError
w.Header().Set("Content-Type", "text/plain")
}

w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
json.NewEncoder(w).Encode(ErrorResponse{message})
_, err = w.Write(body)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing error response: %s\noriginal response: %s\n", err, string(body))
}
}

0 comments on commit 82b630d

Please sign in to comment.