From c89ffd997e0d67f82d087604fc78e9643998c8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 28 Feb 2022 21:11:26 +0100 Subject: [PATCH] Improve `Crystal::Loader` errors --- src/compiler/crystal/loader/unix.cr | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/compiler/crystal/loader/unix.cr b/src/compiler/crystal/loader/unix.cr index 85b64a3a0f3a..bf0b2aec04ec 100644 --- a/src/compiler/crystal/loader/unix.cr +++ b/src/compiler/crystal/loader/unix.cr @@ -20,6 +20,21 @@ class Crystal::Loader alias Handle = Void* + class LoadError + def self.new_dl_error(message) + if char_pointer = LibC.dlerror + new(String.build do |io| + io << message + io << " (" + io.write_string(Slice.new(char_pointer, LibC.strlen(char_pointer))) + io << ")" + end) + else + new message + end + end + end + SHARED_LIBRARY_EXTENSION = {% if flag?(:darwin) %} ".dylib" {% else %} @@ -64,7 +79,11 @@ class Crystal::Loader end def load_file(path : String | ::Path) : Handle - load_file?(path) || raise LoadError.new String.new(LibC.dlerror) + load_file?(path) || raise LoadError.new_dl_error "cannot load #{path}" + end + + def load_library(libname : String) : Handle + load_library?(libname) || raise LoadError.new_dl_error "cannot find -l#{libname}" end private def open_library(path : String)