4
4
#include < unistd.h>
5
5
#endif
6
6
7
+ #include < stdexcept>
7
8
#include " json.hpp"
8
9
#include " context.hpp"
9
10
#include " sass_values.h"
@@ -437,25 +438,38 @@ extern "C" {
437
438
Sass_File_Context* ADDCALL sass_make_file_context (const char * input_path)
438
439
{
439
440
struct Sass_File_Context * ctx = (struct Sass_File_Context *) calloc (1 , sizeof (struct Sass_File_Context ));
440
- if (ctx == 0 ) return 0 ;
441
+ if (ctx == 0 ) { cerr << " Error allocating memory for file context " << endl; return 0 ; }
441
442
ctx->type = SASS_CONTEXT_FILE;
442
- sass_option_set_input_path (ctx, input_path);
443
+ try {
444
+ if (input_path == 0 ) { throw (runtime_error (" File context created without an input path" )); }
445
+ if (*input_path == 0 ) { throw (runtime_error (" File context created with empty input path" )); }
446
+ sass_option_set_input_path (ctx, input_path);
447
+ } catch (...) {
448
+ handle_errors (ctx);
449
+ }
443
450
return ctx;
444
451
}
445
452
446
453
Sass_Data_Context* ADDCALL sass_make_data_context (char * source_string)
447
454
{
448
455
struct Sass_Data_Context * ctx = (struct Sass_Data_Context *) calloc (1 , sizeof (struct Sass_Data_Context ));
449
- if (ctx == 0 ) return 0 ;
456
+ if (ctx == 0 ) { cerr << " Error allocating memory for data context " << endl; return 0 ; }
450
457
ctx->type = SASS_CONTEXT_DATA;
451
- ctx->source_string = source_string;
458
+ try {
459
+ if (source_string == 0 ) { throw (runtime_error (" Data context created without a source string" )); }
460
+ if (*source_string == 0 ) { throw (runtime_error (" Data context created with empty source string" )); }
461
+ ctx->source_string = source_string;
462
+ } catch (...) {
463
+ handle_errors (ctx);
464
+ }
452
465
return ctx;
453
466
}
454
467
455
468
struct Sass_Compiler * ADDCALL sass_make_file_compiler (struct Sass_File_Context * c_ctx)
456
469
{
470
+ if (c_ctx == 0 ) return 0 ;
457
471
struct Sass_Compiler * compiler = (struct Sass_Compiler *) calloc (1 , sizeof (struct Sass_Compiler ));
458
- if (compiler == 0 ) return 0 ;
472
+ if (compiler == 0 ) { cerr << " Error allocating memory for file compiler " << endl; return 0 ; }
459
473
compiler->state = SASS_COMPILER_CREATED;
460
474
compiler->c_ctx = c_ctx;
461
475
Context::Data cpp_opt = Context::Data ();
@@ -466,8 +480,9 @@ extern "C" {
466
480
467
481
struct Sass_Compiler * ADDCALL sass_make_data_compiler (struct Sass_Data_Context * c_ctx)
468
482
{
483
+ if (c_ctx == 0 ) return 0 ;
469
484
struct Sass_Compiler * compiler = (struct Sass_Compiler *) calloc (1 , sizeof (struct Sass_Compiler ));
470
- if (compiler == 0 ) return 0 ;
485
+ if (compiler == 0 ) { cerr << " Error allocating memory for data compiler " << endl; return 0 ; }
471
486
compiler->state = SASS_COMPILER_CREATED;
472
487
compiler->c_ctx = c_ctx;
473
488
Context::Data cpp_opt = Context::Data ();
@@ -478,26 +493,37 @@ extern "C" {
478
493
479
494
int ADDCALL sass_compile_data_context (Sass_Data_Context* data_ctx)
480
495
{
496
+ if (data_ctx == 0 ) return 1 ;
481
497
Sass_Context* c_ctx = data_ctx;
498
+ if (c_ctx->error_status )
499
+ return c_ctx->error_status ;
482
500
Context::Data cpp_opt = Context::Data ();
483
- cpp_opt.source_c_str (data_ctx->source_string );
501
+ try { cpp_opt.source_c_str (data_ctx->source_string ); }
502
+ catch (...) { return handle_errors (c_ctx) || 1 ; }
484
503
return sass_compile_context (c_ctx, cpp_opt);
485
504
}
486
505
487
506
int ADDCALL sass_compile_file_context (Sass_File_Context* file_ctx)
488
507
{
508
+ if (file_ctx == 0 ) return 1 ;
489
509
Sass_Context* c_ctx = file_ctx;
510
+ if (c_ctx->error_status )
511
+ return c_ctx->error_status ;
490
512
Context::Data cpp_opt = Context::Data ();
491
- cpp_opt.entry_point (file_ctx->input_path );
513
+ try { cpp_opt.entry_point (file_ctx->input_path ); }
514
+ catch (...) { return handle_errors (c_ctx) || 1 ; }
492
515
return sass_compile_context (c_ctx, cpp_opt);
493
516
}
494
517
495
518
int ADDCALL sass_compiler_parse (struct Sass_Compiler * compiler)
496
519
{
520
+ if (compiler == 0 ) return 1 ;
497
521
if (compiler->state == SASS_COMPILER_PARSED) return 0 ;
498
522
if (compiler->state != SASS_COMPILER_CREATED) return -1 ;
499
523
if (compiler->c_ctx == NULL ) return 1 ;
500
524
if (compiler->cpp_ctx == NULL ) return 1 ;
525
+ if (compiler->c_ctx ->error_status )
526
+ return compiler->c_ctx ->error_status ;
501
527
compiler->state = SASS_COMPILER_PARSED;
502
528
Context* cpp_ctx = (Context*) compiler->cpp_ctx ;
503
529
// parse the context we have set up (file or data)
@@ -508,12 +534,14 @@ extern "C" {
508
534
509
535
int ADDCALL sass_compiler_execute (struct Sass_Compiler * compiler)
510
536
{
511
- if (compiler == 0 ) return 0 ;
537
+ if (compiler == 0 ) return 1 ;
512
538
if (compiler->state == SASS_COMPILER_EXECUTED) return 0 ;
513
539
if (compiler->state != SASS_COMPILER_PARSED) return -1 ;
514
540
if (compiler->c_ctx == NULL ) return 1 ;
515
541
if (compiler->cpp_ctx == NULL ) return 1 ;
516
542
if (compiler->root == NULL ) return 1 ;
543
+ if (compiler->c_ctx ->error_status )
544
+ return compiler->c_ctx ->error_status ;
517
545
compiler->state = SASS_COMPILER_EXECUTED;
518
546
Context* cpp_ctx = (Context*) compiler->cpp_ctx ;
519
547
Block* root = (Block*) compiler->root ;
0 commit comments