Skip to content

Commit 4640627

Browse files
authored
x.vweb: add error, when static directory does not exist (#20455)
1 parent 0713e39 commit 4640627

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

vlib/x/vweb/static_handler.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn (mut sh StaticHandler) handle_static(directory_path string, root bool) !b
5656
// ```
5757
pub fn (mut sh StaticHandler) host_handle_static(host string, directory_path string, root bool) !bool {
5858
if !os.exists(directory_path) {
59-
return false
59+
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.getwd()}')
6060
}
6161
dir_path := directory_path.trim_space().trim_right('/')
6262
mut mount_path := ''
@@ -84,7 +84,7 @@ pub fn (mut sh StaticHandler) host_mount_static_folder_at(host string, directory
8484
if mount_path.len < 1 || mount_path[0] != `/` {
8585
return error('invalid mount path! The path should start with `/`')
8686
} else if !os.exists(directory_path) {
87-
return error('directory "${directory_path}" does not exist')
87+
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.getwd()}')
8888
}
8989

9090
dir_path := directory_path.trim_right('/')

vlib/x/vweb/tests/static_handler_test.v

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ fn run_app_test() {
4949
}
5050

5151
app.static_mime_types['.what'] = vweb.mime_types['.txt']
52+
53+
if _ := app.handle_static('not_found', true) {
54+
assert false, 'should throw directory not found error'
55+
} else {
56+
assert err.msg().starts_with('directory `not_found` does not exist') == true
57+
}
58+
5259
app.handle_static('testdata', true) or { panic(err) }
5360

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

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

0 commit comments

Comments
 (0)