Skip to content

Commit fd063ef

Browse files
committed
Improve exception message details
1 parent 886f5df commit fd063ef

File tree

20 files changed

+191
-85
lines changed

20 files changed

+191
-85
lines changed

ChangeLog

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
2017-04-10 James J Balamuta <balamut2@illinois.edu>
2+
3+
* inst/include/Rcpp/exceptions.h: Added new exception class generation
4+
macro, reworked existing macros, and removed two exceptions not used.
5+
* inst/include/Rcpp/Environment.h: Improved error handling messages
6+
* inst/include/Rcpp/Function.h: idem
7+
* inst/include/Rcpp/Promise.h: idem
8+
* inst/include/Rcpp/S4.h: idem
9+
* inst/include/Rcpp/String.h: idem
10+
* inst/include/Rcpp/Symbol.h: idem
11+
* inst/include/Rcpp/XPtr.h: idem
12+
* inst/include/Rcpp/as.h: idem
13+
* inst/include/Rcpp/exceptions.h: idem
14+
* inst/include/Rcpp/r_cast.h: idem
15+
* inst/include/Rcpp/api/meat/DottedPairImpl.h: idem
16+
* inst/include/Rcpp/internal/export.h: idem
17+
* inst/include/Rcpp/proxy/DottedPairProxy.h: idem
18+
* inst/include/Rcpp/proxy/SlotProxy.h: idem
19+
* inst/include/Rcpp/vector/Matrix.h: idem
20+
* inst/include/Rcpp/vector/MatrixColumn.h: idem
21+
* inst/include/Rcpp/vector/MatrixRow.h: idem
22+
* inst/include/Rcpp/vector/Vector.h: idem
23+
124
2017-03-28 James J Balamuta <balamut2@illinois.edu>
225

326
* inst/vignettes/Rcpp-FAQ.Rnw: Added "Known Issues" section to FAQ

inst/NEWS.Rd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
\itemize{
88
\item Changes in Rcpp API:
99
\itemize{
10-
\item ...
10+
\item Error messages are now more informative
11+
(James Balamuta in \ghpr{667} addressing \ghit{184}).
1112
}
1213
\item Changes in Rcpp Documentation:
1314
\itemize{

inst/include/Rcpp/Environment.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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" ) ;
43+
throw not_compatible( "Cannot convert to environment (%s -> ENVSXP).", Rf_type2char(TYPEOF(x))) ;
4444
}
4545
}
4646

@@ -115,7 +115,7 @@ namespace Rcpp{
115115
}
116116
return res ;
117117
}
118-
118+
119119
/**
120120
* Get an object from the environment
121121
*
@@ -126,16 +126,16 @@ namespace Rcpp{
126126
SEXP get(Symbol name) const {
127127
SEXP env = Storage::get__() ;
128128
SEXP res = Rf_findVarInFrame( env, name ) ;
129-
129+
130130
if( res == R_UnboundValue ) return R_NilValue ;
131-
131+
132132
/* We need to evaluate if it is a promise */
133133
if( TYPEOF(res) == PROMSXP){
134134
res = Rf_eval( res, env ) ;
135135
}
136136
return res ;
137137
}
138-
138+
139139

140140
/**
141141
* Get an object from the environment or one of its
@@ -157,7 +157,7 @@ namespace Rcpp{
157157
}
158158
return res ;
159159
}
160-
160+
161161
/**
162162
* Get an object from the environment or one of its
163163
* parents
@@ -167,13 +167,13 @@ namespace Rcpp{
167167
SEXP find(Symbol name) const{
168168
SEXP env = Storage::get__() ;
169169
SEXP res = Rf_findVar( name, env ) ;
170-
170+
171171
if( res == R_UnboundValue ) {
172172
// Pass on the const char* to the RCPP_EXCEPTION_CLASS's
173173
// const std::string& requirement
174174
throw binding_not_found(name.c_str()) ;
175175
}
176-
176+
177177
/* We need to evaluate if it is a promise */
178178
if( TYPEOF(res) == PROMSXP){
179179
res = Rf_eval( res, env ) ;

inst/include/Rcpp/Function.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace Rcpp{
4444
Storage::set__(x);
4545
break;
4646
default:
47-
throw not_compatible("cannot convert to function") ;
47+
throw not_compatible("cannot convert %s object to function", Rf_type2char(TYPEOF(x))) ;
4848
}
4949
}
5050

@@ -87,7 +87,7 @@ namespace Rcpp{
8787
SEXP environment() const {
8888
SEXP fun = Storage::get__() ;
8989
if( TYPEOF(fun) != CLOSXP ) {
90-
throw not_a_closure() ;
90+
throw not_a_closure(Rf_type2char(TYPEOF(fun))) ;
9191
}
9292
return CLOENV(fun) ;
9393
}

inst/include/Rcpp/Promise.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Rcpp{
3030

3131
Promise_Impl( SEXP x){
3232
if( TYPEOF(x) != PROMSXP)
33-
throw not_compatible("not a promise") ;
33+
throw not_compatible("Not a promise. Object is of type %s instead of PROMSXP.", Rf_type2char(TYPEOF(x))) ;
3434
Storage::set__(x) ;
3535
}
3636

inst/include/Rcpp/S4.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ namespace Rcpp{
4848
return *this ;
4949
}
5050

51-
/**
52-
* Creates an S4 object of the requested class.
53-
*
54-
* @param klass name of the target S4 class
55-
* @throw not_s4 if klass does not map to a known S4 class
51+
/*!
52+
Creates an S4 object of the requested class.
53+
@param klass name of the target S4 class
54+
@throw S4_creation_error if klass does not map to a known S4 class
5655
*/
5756
S4_Impl( const std::string& klass ){
5857
Shield<SEXP> x( R_do_new_object(R_do_MAKE_CLASS(klass.c_str())) );
@@ -66,6 +65,9 @@ namespace Rcpp{
6665
*/
6766
bool is( const std::string& clazz) const ;
6867

68+
/*!
69+
@throw not_s4 if x is not an S4 class
70+
*/
6971
void update(SEXP x){
7072
if( ! ::Rf_isS4(x) ) throw not_s4() ;
7173
}

inst/include/Rcpp/String.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace Rcpp {
7272
}
7373

7474
if (::Rf_isString(data) && ::Rf_length(data) != 1)
75-
throw ::Rcpp::not_compatible("expecting a single value");
75+
throw ::Rcpp::not_compatible("Expecting a single string value. Received %s object with length %i.", Rf_type2char(TYPEOF(data)), ::Rf_length(data));
7676

7777
valid = true;
7878
buffer_ready = false;

inst/include/Rcpp/Symbol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace Rcpp{
6262
break ;
6363
}
6464
default:
65-
throw not_compatible("cannot convert to symbol (SYMSXP)") ;
65+
throw not_compatible("Cannot object convert to symbol (%s -> SYMSXP)", Rf_type2char(type)) ;
6666
}
6767
}
6868

inst/include/Rcpp/XPtr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class XPtr :
6565
*/
6666
explicit XPtr(SEXP x, SEXP tag = R_NilValue, SEXP prot = R_NilValue) {
6767
if( TYPEOF(x) != EXTPTRSXP )
68-
throw ::Rcpp::not_compatible( "expecting an external pointer" ) ;
68+
throw ::Rcpp::not_compatible( "Expecting an external pointer. Object was a %s instead of EXTPTRSXP", Rf_type2char(TYPEOF(x)) ) ;
6969
Storage::set__(x) ;
7070
R_SetExternalPtrTag( x, tag ) ;
7171
R_SetExternalPtrProtected( x, prot ) ;
@@ -128,7 +128,7 @@ class XPtr :
128128
inline T* checked_get() const {
129129
T* ptr = get();
130130
if (ptr == NULL)
131-
throw ::Rcpp::exception("external pointer is not valid" ) ;
131+
throw ::Rcpp::exception("External pointer is not valid." ) ;
132132
return ptr;
133133
}
134134

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ 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() ;
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__()) ) ;
5757

5858
size_t i=1;
5959
SEXP x = ref.get__() ;
@@ -70,7 +70,7 @@ namespace Rcpp{
7070
template <typename T>
7171
void DottedPairImpl<CLASS>::replace( const int& index, const T& object ) {
7272
CLASS& ref = static_cast<CLASS&>(*this) ;
73-
if( static_cast<R_xlen_t>(index) >= ::Rf_xlength(ref.get__()) ) throw index_out_of_bounds() ;
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__()) ) ;
7474

7575
Shield<SEXP> x( pairlist( object ) );
7676
SEXP y = ref.get__() ;
@@ -84,7 +84,7 @@ namespace Rcpp{
8484
template <typename CLASS>
8585
void DottedPairImpl<CLASS>::remove( const size_t& index ) {
8686
CLASS& ref = static_cast<CLASS&>(*this) ;
87-
if( static_cast<R_xlen_t>(index) >= Rf_xlength(ref.get__()) ) throw index_out_of_bounds() ;
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__()) ) ;
8888
if( index == 0 ){
8989
ref.set__( CDR( ref.get__() ) ) ;
9090
} else{

0 commit comments

Comments
 (0)