Skip to content

Commit 96eb8b5

Browse files
committed
trying to clean up
1 parent 1447154 commit 96eb8b5

File tree

4 files changed

+27
-53
lines changed

4 files changed

+27
-53
lines changed

lib/RenderApp/Controller/IO.pm

+5-9
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ sub raw {
3535
my $file_path = $validatedInput->{sourceFilePath};
3636
my $problem = $c->newProblem( { log => $c->log, read_path => $file_path } );
3737
$problem->{action} = 'fetch source';
38-
return $c->render(
39-
json => $problem->errport(),
40-
status => $problem->{status}
41-
) unless $problem->success();
38+
return $c->exception( $problem->{_message}, $problem->{status} )
39+
unless $problem->success();
4240
$c->render( text => $problem->{problem_contents} );
4341
}
4442

@@ -73,17 +71,15 @@ async sub writer {
7371
}
7472
);
7573

76-
return $c->render(
77-
json => $problem->errport(),
78-
status => $problem->{status}
79-
) unless $problem->success();
74+
return $c->exception( $problem->{_message}, $problem->{status} )
75+
unless $problem->success();
8076

8177
$c->render_later;
8278
my $saveSuccess = await $problem->save;
8379

8480
return ( $saveSuccess ) ?
8581
$c->render( text => $problem->{write_path} ) :
86-
$c->render( json => $problem->errport(), status => $problem->{status} );
82+
$c->exception( $problem->{_message}, $problem->{status} );
8783
}
8884

8985
sub upload {

lib/RenderApp/Controller/Render.pm

+19-27
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ sub parseRequest {
6161
# override key-values in params with those provided in the JWT
6262
@params{ keys %$claims } = values %$claims;
6363
}
64-
warn join(', ', keys %params);
6564
return \%params;
6665
}
6766

@@ -118,41 +117,25 @@ async sub problem {
118117
}
119118

120119
my $problem = $c->newProblem({ log => $c->log, read_path => $file_path, random_seed => $random_seed, problem_contents => $problem_contents });
121-
return $c->exception($problem->{_message}, status => $problem->{status}) unless $problem->success();
120+
return $c->exception($problem->{_message}, $problem->{status})
121+
unless $problem->success();
122122

123123
$inputs_ref->{sourceFilePath} = $problem->{read_path}; # in case the path was updated...
124124

125-
my @input_errs = checkInputs($inputs_ref);
126-
if (@input_errs) {
127-
my $err_log = "Form data submitted for ".$inputs_ref->{sourceFilePath}." contained errors: {";
128-
$err_log .= join "}, {", @input_errs;
129-
$c->log->error($err_log."}");
130-
}
125+
my $input_errs = checkInputs($inputs_ref);
131126

132-
$c->render_later;
127+
$c->render_later; # tell Mojo that this might take a while
133128
my $ww_return_json = await $problem->render($inputs_ref);
134129

135-
unless ($problem->success()) {
136-
$c->log->warn($problem->{_message});
137-
return $c->render(
138-
json => $problem->errport(),
139-
status => $problem->{status}
140-
);
141-
}
130+
return $c->exception( $problem->{_message}, $problem->{status} )
131+
unless $problem->success();
142132

143133
my $ww_return_hash = decode_json($ww_return_json);
144-
my @output_errs = checkOutputs($ww_return_hash);
145-
if (@output_errs) {
146-
my $err_log = "Output from rendering ".$inputs_ref->{sourceFilePath}." contained errors: {";
147-
$err_log .= join "}, {", @output_errs;
148-
$c->log->error($err_log."}");
149-
}
134+
my $output_errs = checkOutputs($ww_return_hash);
150135

151-
$ww_return_hash->{debug}->{render_warn} = [@input_errs, @output_errs];
136+
$ww_return_hash->{debug}->{render_warn} = [$input_errs, $output_errs];
152137

153138
# if answers are submitted and there is a provided answerURL...
154-
155-
156139
if ($inputs_ref->{JWTanswerURL} && $ww_return_hash->{answerJWT} && $inputs_ref->{submitAnswers}) {
157140
my $answerJWTresponse = {
158141
iss => $ENV{SITE_HOST},
@@ -220,7 +203,11 @@ sub checkInputs {
220203
push @errs, $err;
221204
}
222205
}
223-
return @errs;
206+
return "Form data submitted for "
207+
. $inputs_ref->{sourceFilePath}
208+
. " contained errors: {"
209+
. join "}, {", @errs
210+
. "}";
224211
}
225212

226213
sub checkOutputs {
@@ -248,14 +235,19 @@ sub checkOutputs {
248235
}
249236
}
250237
}
251-
return @errs;
238+
return
239+
"Output from rendering "
240+
. $outputs_ref->{sourceFilePath}
241+
. " contained errors: {"
242+
. join "}, {", @errs . "}";
252243
}
253244

254245
sub exception {
255246
my $c = shift;
256247
my $id = $c->logID;
257248
my $message = "[$id] " . shift;
258249
my $status = shift;
250+
$c->log->error("($status) EXCEPTION: $message");
259251
return $c->respond_to(
260252
json => { json => {
261253
message => $message,

lib/RenderApp/Controller/RenderProblem.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ sub fake_user {
553553
first_name => 'Motoko',
554554
last_name => 'Kusanagi',
555555
email_address => '[email protected]',
556-
student_id => '',
556+
student_id => '123456789',
557557
section => '9',
558558
recitation => '',
559559
comment => '',

lib/RenderApp/Model/Problem.pm

+2-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use RenderApp::Controller::RenderProblem;
2828
# - load (overwrite problem_contents with contents of file at read_path)
2929
## Error handling
3030
# - success (checks for internal errors and sets error code and message)
31-
# - errport (delivers a hash reference to be rendered as JSON)
31+
# - exception (renders JSON/HTML message w/ status & logs)
3232

3333
our $codes = {
3434
400 => 'Bad Request',
@@ -54,7 +54,6 @@ sub new {
5454

5555
sub _init {
5656
my ( $self, $args ) = @_;
57-
my $availability = 'private';
5857
$self->{log} = $args->{log} if $args->{log};
5958

6059
my $read_path = $args->{read_path} || '';
@@ -222,15 +221,13 @@ sub render {
222221
})->catch(sub {
223222
$self->{exception} = Mojo::Exception->new(shift)->trace;
224223
$self->{_error} = "500 Render failed: " . $self->{exception}->message;
225-
$self->{log}->debug( $self->{_error} );
226224
});
227225
return $renderPromise;
228226
}
229227

230228
sub success {
231229
my $self = shift;
232-
# my $report = ( $self->{_error} =~ /\S/ ) ? $self->{_error} : 'NO ERRORS';
233-
# $self->{log}->debug('CHECKING SUCCESS... ' . $report);
230+
my $report = ( $self->{_error} =~ /\S/ ) ? $self->{_error} : 'NO ERRORS';
234231
return 1 unless $self->{_error} =~ /\S/;
235232
my ( $code, $mesg ) = split( / /, $self->{_error}, 2 );
236233
$self->{status} = $code;
@@ -239,17 +236,6 @@ sub success {
239236
return 0;
240237
}
241238

242-
sub errport {
243-
my $self = shift;
244-
return unless $self->{_error};
245-
my $err_ref = {
246-
statusCode => $self->{status},
247-
status => $self->{_error},
248-
message => $self->{_message},
249-
};
250-
return $err_ref;
251-
}
252-
253239
sub DESTROY {
254240
my $self = shift;
255241
my $duration = time - $self->{start};

0 commit comments

Comments
 (0)