Skip to content

Commit

Permalink
feat: custom static error pages (#2)
Browse files Browse the repository at this point in the history
* feat: ✨ Detect custom error pages

Closes #1

* more descriptive comment

* feat: use IO.copy instead of reading the file

* chore: avoid building string twice

* feat: spec

* chore: format

---------

Co-authored-by: Evangelos Paterakis <evan@geopjr.dev>
  • Loading branch information
ThatOneCalculator and GeopJr authored Jun 28, 2023
1 parent 70bddc7 commit 3438ba4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ They are built & published by our lovely [actions](https://github.com/GeopJr/sab
## Usage

```
sabo-tabby v1.0.0
sabo-tabby v1.1.0
Usage: sabo-tabby [arguments]
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sabo-tabby
version: 1.0.0
version: 1.1.0

authors:
- Evangelos Paterakis <evan@geopjr.dev>
Expand Down
22 changes: 22 additions & 0 deletions spec/static_file_handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ describe Sabo::Tabby::StaticFileHandler do
response.headers["Content-Encoding"]?.should be_nil
end

it "should serve custom static error pages" do
page_content = "🫖"

# Create custom static error page
current_public_folder = Sabo::Tabby.config.public_folder
folder = Path[Dir.tempdir, Random::Secure.hex(5)]
Dir.mkdir(folder)
Sabo::Tabby.config.public_folder = folder.to_s

custom_page_path = Path[folder, "404.html"]
File.write(custom_page_path, page_content)

response = handle_request HTTP::Request.new("GET", "/I_DO_NOT_EXIST")
response.status_code.should eq(404)
response.body.should eq(page_content)

# Cleanup
Sabo::Tabby.config.public_folder = current_public_folder
File.delete(custom_page_path)
Dir.delete(folder)
end

it "should not serve a not found file" do
response = handle_request HTTP::Request.new("GET", "/not_found_file.txt")
response.status_code.should eq(404)
Expand Down
10 changes: 8 additions & 2 deletions src/sabo-tabby/error_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ module Sabo::Tabby
@status_message = message ||= @status.description
self.headers.add "Server", Sabo::Tabby::SERVER_HEADER if Sabo::Tabby.config.server_header

# If HTML pages are enabled, call `error_page` else return a basic text/plain one.
if Sabo::Tabby.config.error_page
# If it exists, use file in the public dir matching error_code.html (eg 404.html).
static_error_page = Path[Sabo::Tabby.config.public_folder, "#{status_code}.html"]
if File.exists?(static_error_page)
self.content_type = "text/html; charset=UTF-8"
IO.copy File.open(static_error_page), self

# If HTML pages are enabled, call `error_page` else return a basic text/plain one.
elsif Sabo::Tabby.config.error_page
self.content_type = "text/html; charset=UTF-8"
self << error_page(@status.code, @status_message.to_s) << '\n'
else
Expand Down

0 comments on commit 3438ba4

Please sign in to comment.