-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package infrastructure | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/go-http-utils/headers" | ||
"github.com/pterm/pterm" | ||
"github.com/pterm/pterm/putils" | ||
"github.com/samber/lo" | ||
) | ||
|
||
var style = pterm.Style{} | ||
|
||
func HTTPError(writer http.ResponseWriter, err error) { | ||
header := writer.Header() | ||
header.Set(headers.ContentType, "text/plain; charset=utf-8") | ||
header.Set(headers.XContentTypeOptions, "nosniff") | ||
|
||
writer.WriteHeader(http.StatusInternalServerError) | ||
message := fmt.Sprintf("%d Error", http.StatusInternalServerError) | ||
|
||
writeLine(writer) | ||
writeLine(writer, pageHeader(message)) | ||
writeLine(writer) | ||
writeLine(writer, fmt.Sprintf("Occurred error: %s", err)) | ||
} | ||
|
||
func pageHeader(message string) string { | ||
letters := putils.LettersFromStringWithStyle(message, &style) | ||
text, err := pterm.DefaultBigText.WithLetters(letters).Srender() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return text | ||
} | ||
|
||
func writeLine(writer io.Writer, data ...string) { | ||
if _, err := fmt.Fprintln(writer, lo.ToAnySlice(data)...); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package infrastructure_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-http-utils/headers" | ||
|
||
"github.com/evg4b/uncors/internal/infrastructure" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const expectedPage = ` | ||
███████ ██████ ██████ ███████ ██████ ██████ ██████ ██████ | ||
██ ██ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ | ||
███████ ██ ██ ██ ██ ██ ██ █████ ██████ ██████ ██ ██ ██████ | ||
██ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ | ||
███████ ██████ ██████ ███████ ██ ██ ██ ██ ██████ ██ ██ | ||
Occurred error: net/http: abort Handler | ||
` | ||
|
||
func TestHttpError(t *testing.T) { | ||
recorder := httptest.NewRecorder() | ||
infrastructure.HTTPError(recorder, http.ErrAbortHandler) | ||
|
||
t.Run("write correct page", func(t *testing.T) { | ||
assert.Equal(t, expectedPage, testutils.ReadBody(t, recorder)) | ||
}) | ||
|
||
t.Run("write correct headers", func(t *testing.T) { | ||
header := recorder.Header() | ||
|
||
assert.NotNil(t, header[headers.ContentType]) | ||
assert.NotNil(t, header[headers.XContentTypeOptions]) | ||
}) | ||
} |