Skip to content

Commit ba4403d

Browse files
committed
Revised exception messages
1 parent a67d16a commit ba4403d

File tree

16 files changed

+181
-60
lines changed

16 files changed

+181
-60
lines changed

ChangeLog

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
2017-04-14 James J Balamuta <balamut2@illinois.edu>
2+
3+
* inst/include/Rcpp/Environment.h: Modified formatting of new exception
4+
messages to be more concise.
5+
* inst/include/Rcpp/exceptions.h: idem
6+
* inst/include/Rcpp/Function.h: idem
7+
* inst/include/Rcpp/Promise.h: idem
8+
* inst/include/Rcpp/String.h: idem
9+
* inst/include/Rcpp/Symbol.h: idem
10+
* inst/include/Rcpp/XPtr.h: idem
11+
* inst/include/Rcpp/as.h: idem
12+
* inst/include/Rcpp/r_cast.h: idem
13+
* inst/include/Rcpp/api/meat/DottedPairImpl.h: idem
14+
* inst/include/Rcpp/internal/export.h: idem
15+
* inst/include/Rcpp/proxy/DottedPairProxy.h: idem
16+
* inst/include/Rcpp/vector/MatrixColumn.h: idem
17+
* inst/include/Rcpp/vector/MatrixRow.h: idem
18+
* inst/include/Rcpp/vector/Vector.h: idem
19+
120
2017-04-11 Dirk Eddelbuettel <edd@debian.org>
221

322
* inst/inst/unitTests/testRcppClass/src/init.c (R_init_testRcppClass): Call
@@ -43,6 +62,7 @@
4362
* inst/include/Rcpp/macros/macros.h: Idem
4463
* inst/unitTests/cpp/exceptions.cpp: Idem
4564
* inst/unitTests/runit.exceptions.R: Idem
65+
4666
2017-04-10 James J Balamuta <balamut2@illinois.edu>
4767

4868
* inst/include/Rcpp/exceptions.h: Added new exception class generation
@@ -55,7 +75,6 @@
5575
* inst/include/Rcpp/Symbol.h: idem
5676
* inst/include/Rcpp/XPtr.h: idem
5777
* inst/include/Rcpp/as.h: idem
58-
* inst/include/Rcpp/exceptions.h: idem
5978
* inst/include/Rcpp/r_cast.h: idem
6079
* inst/include/Rcpp/api/meat/DottedPairImpl.h: idem
6180
* inst/include/Rcpp/internal/export.h: idem

inst/include/Rcpp/Environment.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ namespace Rcpp{
4040
Shield<SEXP> res( Rcpp_eval( Rf_lang2( asEnvironmentSym, x ) ) );
4141
return res ;
4242
} catch( const eval_error& ex){
43-
throw not_compatible( "Cannot convert to environment (%s -> ENVSXP).", Rf_type2char(TYPEOF(x))) ;
43+
const char* fmt =
44+
"Cannot convert object to an environment: "
45+
"[type:%s; target=ENVSXP].";
46+
throw not_compatible(fmt, Rf_type2char(TYPEOF(x))) ;
4447
}
4548
}
4649

inst/include/Rcpp/Function.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ namespace Rcpp{
4444
Storage::set__(x);
4545
break;
4646
default:
47-
throw not_compatible("cannot convert %s object to function", Rf_type2char(TYPEOF(x))) ;
47+
const char* fmt =
48+
"Cannot convert object to a function:"
49+
"[type=%s; target=CLOSXP, SPECIALSXP, or BUILTINSXP].";
50+
throw not_compatible(fmt, Rf_type2char(TYPEOF(x))) ;
4851
}
4952
}
5053

inst/include/Rcpp/Promise.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ namespace Rcpp{
2929
RCPP_GENERATE_CTOR_ASSIGN(Promise_Impl)
3030

3131
Promise_Impl( SEXP x){
32-
if( TYPEOF(x) != PROMSXP)
33-
throw not_compatible("Not a promise. Object is of type %s instead of PROMSXP.", Rf_type2char(TYPEOF(x))) ;
32+
if( TYPEOF(x) != PROMSXP){
33+
const char* fmt = "Not a promise: [type = %s].";
34+
throw not_compatible(fmt, Rf_type2char(TYPEOF(x))) ;
35+
}
3436
Storage::set__(x) ;
3537
}
3638

inst/include/Rcpp/String.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ namespace Rcpp {
7171
data = charsxp;
7272
}
7373

74-
if (::Rf_isString(data) && ::Rf_length(data) != 1)
75-
throw ::Rcpp::not_compatible("Expecting a single string value. Received %s object with length %i.", Rf_type2char(TYPEOF(data)), ::Rf_length(data));
74+
if (::Rf_isString(data) && ::Rf_length(data) != 1){
75+
const char* fmt =
76+
"Expecting a single string value: [type=%s; extent=%i].";
77+
throw ::Rcpp::not_compatible(fmt,
78+
Rf_type2char(TYPEOF(data)),
79+
::Rf_length(data));
80+
81+
}
7682

7783
valid = true;
7884
buffer_ready = false;

inst/include/Rcpp/Symbol.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ namespace Rcpp{
6262
break ;
6363
}
6464
default:
65-
throw not_compatible("Cannot object convert to symbol (%s -> SYMSXP)", Rf_type2char(type)) ;
65+
const char* fmt =
66+
"Cannot convert object to a symbol (%s -> SYMSXP).";
67+
throw not_compatible(fmt, Rf_type2char(type)) ;
6668
}
6769
}
6870

inst/include/Rcpp/XPtr.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ class XPtr :
6464
* @param xp external pointer to wrap
6565
*/
6666
explicit XPtr(SEXP x, SEXP tag = R_NilValue, SEXP prot = R_NilValue) {
67-
if( TYPEOF(x) != EXTPTRSXP )
68-
throw ::Rcpp::not_compatible( "Expecting an external pointer. Object was a %s instead of EXTPTRSXP", Rf_type2char(TYPEOF(x)) ) ;
67+
if( TYPEOF(x) != EXTPTRSXP ) {
68+
const char* fmt = "Expecting an external pointer: [type=%s].";
69+
throw ::Rcpp::not_compatible(fmt, Rf_type2char(TYPEOF(x)) ) ;
70+
}
6971
Storage::set__(x) ;
7072
R_SetExternalPtrTag( x, tag ) ;
7173
R_SetExternalPtrProtected( x, prot ) ;

inst/include/Rcpp/api/meat/DottedPairImpl.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ namespace Rcpp{
5353
} else {
5454
if( ref.isNULL( ) ) throw index_out_of_bounds() ;
5555

56-
if( static_cast<R_xlen_t>(index) > ::Rf_xlength(ref.get__()) ) throw index_out_of_bounds("index is out of bounds. Requested %i with %i available", static_cast<R_xlen_t>(index), ::Rf_xlength(ref.get__()) ) ;
56+
if( static_cast<R_xlen_t>(index) > ::Rf_xlength(ref.get__()) ) {
57+
const char* fmt = "Index is out of bounds: [index=%s; extent=%s].";
58+
throw index_out_of_bounds(fmt,
59+
static_cast<R_xlen_t>(index),
60+
::Rf_xlength(ref.get__()) ) ;
61+
}
5762

5863
size_t i=1;
5964
SEXP x = ref.get__() ;
@@ -70,7 +75,12 @@ namespace Rcpp{
7075
template <typename T>
7176
void DottedPairImpl<CLASS>::replace( const int& index, const T& object ) {
7277
CLASS& ref = static_cast<CLASS&>(*this) ;
73-
if( static_cast<R_xlen_t>(index) >= ::Rf_xlength(ref.get__()) ) throw index_out_of_bounds("index is out of bounds. Requested %i with %i available", static_cast<R_xlen_t>(index), ::Rf_xlength(ref.get__()) ) ;
78+
if( static_cast<R_xlen_t>(index) >= ::Rf_xlength(ref.get__()) ) {
79+
const char* fmt = "Index is out of bounds: [index=%s; extent=%s].";
80+
throw index_out_of_bounds(fmt,
81+
static_cast<R_xlen_t>(index),
82+
::Rf_xlength(ref.get__()) ) ;
83+
}
7484

7585
Shield<SEXP> x( pairlist( object ) );
7686
SEXP y = ref.get__() ;
@@ -84,7 +94,13 @@ namespace Rcpp{
8494
template <typename CLASS>
8595
void DottedPairImpl<CLASS>::remove( const size_t& index ) {
8696
CLASS& ref = static_cast<CLASS&>(*this) ;
87-
if( static_cast<R_xlen_t>(index) >= Rf_xlength(ref.get__()) ) throw index_out_of_bounds("index is out of bounds. Requested %i with %i available", static_cast<R_xlen_t>(index), ::Rf_xlength(ref.get__()) ) ;
97+
if( static_cast<R_xlen_t>(index) >= Rf_xlength(ref.get__()) ) {
98+
const char* fmt = "Index is out of bounds: [index=%s; extent=%s].";
99+
throw index_out_of_bounds(fmt,
100+
static_cast<R_xlen_t>(index),
101+
::Rf_xlength(ref.get__()) ) ;
102+
}
103+
88104
if( index == 0 ){
89105
ref.set__( CDR( ref.get__() ) ) ;
90106
} else{

inst/include/Rcpp/as.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ namespace Rcpp {
2929
namespace internal {
3030

3131
template <typename T> T primitive_as(SEXP x) {
32-
if (::Rf_length(x) != 1) throw ::Rcpp::not_compatible("Expecting a single value. Object contained %i values.", ::Rf_length(x));
32+
if (::Rf_length(x) != 1) {
33+
const char* fmt = "Expecting a single value: [extent=%i].";
34+
throw ::Rcpp::not_compatible(fmt, ::Rf_length(x));
35+
}
3336
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
3437
Shield<SEXP> y(r_cast<RTYPE>(x));
3538
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
@@ -43,10 +46,14 @@ namespace Rcpp {
4346

4447
inline const char* check_single_string(SEXP x) {
4548
if (TYPEOF(x) == CHARSXP) return CHAR(x);
46-
if (! ::Rf_isString(x))
47-
throw ::Rcpp::not_compatible("Expecting a string. Received %s instead of CHARSXP or STRSXP.", Rf_type2char(TYPEOF(x)));
48-
if (Rf_length(x) != 1)
49-
throw ::Rcpp::not_compatible("Expecting a single value. Object contained %i values.", ::Rf_length(x));
49+
if (! ::Rf_isString(x)) {
50+
const char* fmt = "Expecting a string: [type=%s].";
51+
throw ::Rcpp::not_compatible(fmt, Rf_type2char(TYPEOF(x)));
52+
}
53+
if (Rf_length(x) != 1) {
54+
const char* fmt = "Expecting a single value: [extent=%i].";
55+
throw ::Rcpp::not_compatible(fmt, ::Rf_length(x));
56+
}
5057
return CHAR(STRING_ELT(::Rcpp::r_cast<STRSXP>(x), 0));
5158
}
5259

@@ -66,10 +73,12 @@ namespace Rcpp {
6673

6774
template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_RcppString_tag) {
6875
if (! ::Rf_isString(x)) {
69-
throw ::Rcpp::not_compatible("Expecting a string. Received %s instead of STRSXP.", Rf_type2char(TYPEOF(x)));
76+
const char* fmt = "Expecting a string: [type=%s].";
77+
throw ::Rcpp::not_compatible(fmt, Rf_type2char(TYPEOF(x)));
7078
}
7179
if (Rf_length(x) != 1) {
72-
throw ::Rcpp::not_compatible("Expecting a single value. Object contained %i values.", ::Rf_length(x));
80+
const char* fmt = "Expecting a single value: [extent=%i].";
81+
throw ::Rcpp::not_compatible(fmt, ::Rf_length(x));
7382
}
7483
return STRING_ELT(::Rcpp::r_cast<STRSXP>(x), 0);
7584
}

inst/include/Rcpp/exceptions.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,27 +157,27 @@ namespace Rcpp {
157157
virtual const char* what() const throw() { return __MESSAGE__ ; } \
158158
} ;
159159

160-
RCPP_SIMPLE_EXCEPTION_CLASS(not_a_matrix, "not a matrix")
161-
RCPP_SIMPLE_EXCEPTION_CLASS(parse_error, "parse error")
162-
RCPP_SIMPLE_EXCEPTION_CLASS(not_s4, "not an S4 object") // #nocov start
163-
RCPP_SIMPLE_EXCEPTION_CLASS(not_reference, "not an S4 object of a reference class")
164-
RCPP_SIMPLE_EXCEPTION_CLASS(not_initialized, "C++ object not initialized (missing default constructor?)")
165-
RCPP_SIMPLE_EXCEPTION_CLASS(no_such_function, "no such function")
166-
RCPP_SIMPLE_EXCEPTION_CLASS(unevaluated_promise, "promise not yet evaluated")
167-
168-
RCPP_EXCEPTION_CLASS(no_such_slot, std::string("no such slot: ") + message )
169-
RCPP_EXCEPTION_CLASS(S4_creation_error, std::string("error creating object of S4 class: ") + message )
170-
RCPP_EXCEPTION_CLASS(reference_creation_error, std::string("error creating object of reference class: ") + message )
171-
RCPP_EXCEPTION_CLASS(no_such_binding, std::string("no such binding: '") + message + "'" )
172-
RCPP_EXCEPTION_CLASS(binding_not_found, std::string("binding not found: '") + message + "'" )
173-
RCPP_EXCEPTION_CLASS(binding_is_locked, std::string("binding is locked: '") + message + "'" )
174-
RCPP_EXCEPTION_CLASS(no_such_namespace, std::string("no such namespace: '") + message + "'" )
175-
RCPP_EXCEPTION_CLASS(function_not_exported, std::string("function not exported: ") + message)
160+
RCPP_SIMPLE_EXCEPTION_CLASS(not_a_matrix, "Not a matrix.")
161+
RCPP_SIMPLE_EXCEPTION_CLASS(parse_error, "Parse error.")
162+
RCPP_SIMPLE_EXCEPTION_CLASS(not_s4, "Not an S4 object.") // #nocov start
163+
RCPP_SIMPLE_EXCEPTION_CLASS(not_reference, "Not an S4 object of a reference class.")
164+
RCPP_SIMPLE_EXCEPTION_CLASS(not_initialized, "C++ object not initialized. (Missing default constructor?)")
165+
RCPP_SIMPLE_EXCEPTION_CLASS(no_such_function, "No such function.")
166+
RCPP_SIMPLE_EXCEPTION_CLASS(unevaluated_promise, "Promise not yet evaluated.")
167+
168+
RCPP_EXCEPTION_CLASS(no_such_slot, std::string("No such slot: ") + message + "." )
169+
RCPP_EXCEPTION_CLASS(S4_creation_error, std::string("Error creating object of S4 class: ") + message + "." )
170+
RCPP_EXCEPTION_CLASS(reference_creation_error, std::string("Error creating object of reference class: ") + message + ".")
171+
RCPP_EXCEPTION_CLASS(no_such_binding, std::string("No such binding: '") + message + "'." )
172+
RCPP_EXCEPTION_CLASS(binding_not_found, std::string("Binding not found: '") + message + "'." )
173+
RCPP_EXCEPTION_CLASS(binding_is_locked, std::string("Binding is locked: '") + message + "'." )
174+
RCPP_EXCEPTION_CLASS(no_such_namespace, std::string("No such namespace: '") + message + "'." )
175+
RCPP_EXCEPTION_CLASS(function_not_exported, std::string("Function not exported: ") + message + "." )
176176
RCPP_EXCEPTION_CLASS(eval_error, message )
177-
RCPP_EXCEPTION_CLASS(not_a_closure, std::string("not a closure. Object is of type ") + message + "." )
177+
RCPP_EXCEPTION_CLASS(not_a_closure, std::string("Not a closure: [type=") + message + "]." )
178178

179-
RCPP_ADVANCED_EXCEPTION_CLASS(not_compatible, "not compatible")
180-
RCPP_ADVANCED_EXCEPTION_CLASS(index_out_of_bounds, "index out of bounds") // #nocov end
179+
RCPP_ADVANCED_EXCEPTION_CLASS(not_compatible, "Not compatible.")
180+
RCPP_ADVANCED_EXCEPTION_CLASS(index_out_of_bounds, "Index out of bounds.") // #nocov end
181181

182182
#undef RCPP_SIMPLE_EXCEPTION_CLASS
183183
#undef RCPP_EXCEPTION_CLASS

0 commit comments

Comments
 (0)