Skip to content

Commit

Permalink
Include the file path in error messages when running external files
Browse files Browse the repository at this point in the history
Should hopefully fix sonic-pi-net#2336
  • Loading branch information
SunderB committed May 22, 2021
1 parent a7612c8 commit 41e4689
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
6 changes: 3 additions & 3 deletions app/server/ruby/bin/sonic-pi-server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@

# read in init.rb if exists
if File.exists?(init_path)
sp.__spider_eval(File.read(init_path), silent: true)
sp.__spider_eval(File.read(init_path), type: :file, name: init_path, silent: true)
else
STDOUT.puts "Could not find init.rb file: #{init_path} "
end
Expand Down Expand Up @@ -375,7 +375,7 @@
server.add_method("/run-code") do |args|
gui_id = args[0]
code = args[1].force_encoding("utf-8")
sp.__spider_eval code
sp.__spider_eval(code, type: :eval, name: "osc-/run-code")
end

server.add_method("/save-and-run-buffer") do |args|
Expand All @@ -384,7 +384,7 @@
code = args[2].force_encoding("utf-8")
workspace = args[3]
sp.__save_buffer(buffer_id, code)
sp.__spider_eval code, {workspace: workspace}
sp.__spider_eval(code, type: :workspace, name: workspace)
end

server.add_method("/save-buffer") do |args|
Expand Down
7 changes: 5 additions & 2 deletions app/server/ruby/lib/sonicpi/lang/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def with_swing(*args, &blk)
def run_file(path)
path = File.expand_path(path.to_s)
raise IOError, "Unable to run file - no file found with path: #{path}" unless File.exist?(path)
__spider_eval(File.read(path))
__spider_eval(File.read(path), type: :file, name: path)
end
doc name: :run_file,
introduced: Version.new(2,11,0),
Expand All @@ -477,7 +477,7 @@ def run_file(path)
run_file \"~/path/to/sonic-pi-code.rb\" #=> will run the contents of this file"]

def run_code(code)
__spider_eval(code.to_s)
__spider_eval(code.to_s, type: :eval, name: "run_code")
end
doc name: :run_code,
introduced: Version.new(2,11,0),
Expand Down Expand Up @@ -4785,6 +4785,9 @@ def assert_similar(a, b, msg=nil)
def load_buffer(path)
path = File.expand_path(path.to_s)
raise IOError, "Unable to load buffer - no file found with path: #{path}" unless File.exist?(path)

raise RuntimeError, "Unable to retrieve the current workspace. Current job info: #{__current_job_info}" unless __current_job_info[:workspace]

buf = __current_job_info[:workspace]
__info "loading #{buf} with #{path}"
__replace_buffer(buf, File.read(path))
Expand Down
50 changes: 32 additions & 18 deletions app/server/ruby/lib/sonicpi/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ module RuntimeMethods
def load_snippets(path=snippets_path, quiet=false)
path = File.expand_path(path)
Dir["#{path}/**/*.sps"].each do |p|

lines = File.readlines(p)
key = nil
completion = ""
Expand Down Expand Up @@ -315,14 +314,8 @@ def __error(e, m=nil)
info = __current_job_info
err_msg.gsub(/for #<SonicPiSpiderUser[a-z0-9:]+>/, '')
res = ""
w = info[:workspace]
w = info[:display_name]
if line != -1

# TODO: Remove this hack when we have projects
w = normalise_buffer_name(w)
w = "buffer " + w
# TODO: end of hack

res = res + "[#{w}, line #{line}]"
else
res = res + "[#{w}]"
Expand Down Expand Up @@ -792,10 +785,12 @@ def __spider_eval(code, info={})
firstline = 1
firstline -= code.lines.to_a.take_while{|l| l.include? "#__nosave__"}.count
start_t_prom = Promise.new
info[:workspace] = 'eval' unless info[:workspace]

display_name = get_spider_eval_display_name(info[:type], info[:name])

info[:workspace].freeze
info[:name].freeze
info.freeze
display_name.freeze

job_in_thread = nil
job = Thread.new do
Expand Down Expand Up @@ -827,7 +822,7 @@ def __spider_eval(code, info={})
code = PreParser.preparse(code, SonicPi::Lang::Core.vec_fns)

job_in_thread = in_thread seed: 0 do
eval(code, nil, info[:workspace], firstline)
eval(code, nil, info[:name], firstline)
end
__schedule_delayed_blocks_and_messages!
rescue Stop => e
Expand All @@ -841,13 +836,7 @@ def __spider_eval(code, info={})
if line
line = line.to_i

# TODO: Remove this hack when we have projects
w = info[:workspace]
w = normalise_buffer_name(w)
w = "buffer #{w}"
# TODO: end of hack

err_msg = "[#{w}, line #{line}] \n #{message}"
err_msg = "[#{display_name}, line #{line}] \n #{message}"
error_line = code.lines.to_a[line - firstline] || ""
else
line = -1
Expand Down Expand Up @@ -1278,6 +1267,31 @@ def beautify_ruby_source(source)
res = res.gsub(') ___SONIC_PI_RND_TMP_PLACEHOLDER___ /', ')/')
res = res.gsub('] ___SONIC_PI_SQR_TMP_PLACEHOLDER___ /', ']/')
end

def get_spider_eval_display_name(type, name)
case info[:type]
when :workspace
"buffer #{normalise_buffer_name(name)}"
when :file
name
when :eval
normalise_eval_name(name)
end
end

def normalise_eval_name(name)
if (name.nil?)
return "unnamed eval"
end

norm = case name
when "osc-/run-code"
"eval"
else
name
end
return norm
end

def normalise_buffer_name(name)
norm = case name
Expand Down

0 comments on commit 41e4689

Please sign in to comment.