Bug 17502: Throw some exceptions in output_pref
[koha-ffzg.git] / Koha / DateUtils.pm
index 65afaea..66ad264 100644 (file)
@@ -19,10 +19,9 @@ package Koha::DateUtils;
 use Modern::Perl;
 use DateTime;
 use C4::Context;
-use Carp;
+use Koha::Exceptions;
 
 use base 'Exporter';
-use version; our $VERSION = qv('1.0.0');
 
 our @EXPORT = (
     qw( dt_from_string output_pref format_sqldatetime )
@@ -210,14 +209,16 @@ sub output_pref {
         $dt = $params;
     }
 
-    carp "output_pref should not be called with both dt and str parameters"
-        and return
-            if $dt and $str;
+    Koha::Exceptions::WrongParameter->throw( 'output_pref should not be called with both dt and str parameter' ) if $dt and $str;
 
-    $dt = eval { dt_from_string( $str ) } if $str;
-    carp "Invalid date '$str' passed to output_pref\n" if $@;
+    if ( $str ) {
+        local $@;
+        $dt = eval { dt_from_string( $str ) };
+        Koha::Exceptions::WrongParameter->throw("Invalid date '$str' passed to output_pref" ) if $@;
+    }
 
-    return unless defined $dt;
+    return if !defined $dt; # NULL date
+    Koha::Exceptions::WrongParameter->throw( 'dt is not a datetime' )  if ref($dt) ne 'DateTime';
 
     # FIXME: see bug 13242 => no TZ for dates 'infinite'
     if ( $dt->ymd !~ /^9999/ ) {