Skip to content

Commit

Permalink
x.vweb: add error, when static directory does not exist (#20455)
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper64 committed Jan 10, 2024
1 parent 0713e39 commit 4640627
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions vlib/x/vweb/static_handler.v
Expand Up @@ -56,7 +56,7 @@ pub fn (mut sh StaticHandler) handle_static(directory_path string, root bool) !b
// ```
pub fn (mut sh StaticHandler) host_handle_static(host string, directory_path string, root bool) !bool {
if !os.exists(directory_path) {
return false
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.getwd()}')
}
dir_path := directory_path.trim_space().trim_right('/')
mut mount_path := ''
Expand Down Expand Up @@ -84,7 +84,7 @@ pub fn (mut sh StaticHandler) host_mount_static_folder_at(host string, directory
if mount_path.len < 1 || mount_path[0] != `/` {
return error('invalid mount path! The path should start with `/`')
} else if !os.exists(directory_path) {
return error('directory "${directory_path}" does not exist')
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.getwd()}')
}

dir_path := directory_path.trim_right('/')
Expand Down
9 changes: 8 additions & 1 deletion vlib/x/vweb/tests/static_handler_test.v
Expand Up @@ -49,6 +49,13 @@ fn run_app_test() {
}

app.static_mime_types['.what'] = vweb.mime_types['.txt']

if _ := app.handle_static('not_found', true) {
assert false, 'should throw directory not found error'
} else {
assert err.msg().starts_with('directory `not_found` does not exist') == true
}

app.handle_static('testdata', true) or { panic(err) }

if _ := app.mount_static_folder_at('testdata', 'static') {
Expand All @@ -60,7 +67,7 @@ fn run_app_test() {
if _ := app.mount_static_folder_at('not_found', '/static') {
assert true == false, 'should throw mount path does not exist error'
} else {
assert err.msg() == 'directory "not_found" does not exist'
assert err.msg().starts_with('directory `not_found` does not exist') == true
}

app.mount_static_folder_at('testdata', '/static') or { panic(err) }
Expand Down

0 comments on commit 4640627

Please sign in to comment.