Skip to content

Commit 9c1cf75

Browse files
Andrea Schiaviniplusjade
authored andcommitted
Added exception handling to display a user friendly message when time parse goes wrong
1 parent 1a9411a commit 9c1cf75

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Rakefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ task :post do
4646
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
4747
title = ENV["title"] || "new-post"
4848
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
49-
date = (Time.parse(ENV['date']) || Time.now).strftime('%Y-%m-%d')
49+
begin
50+
date = (Time.parse(ENV['date']) || Time.now).strftime('%Y-%m-%d')
51+
rescue Exception => e
52+
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
53+
exit -1
54+
end
5055
filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
5156
if File.exist?(filename)
5257
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'

0 commit comments

Comments
 (0)