Skip to content

Commit

Permalink
Improve handling of card request not found/wrong state errors (see #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasmalacofilho committed Jan 12, 2018
1 parent cb90e8e commit 635548a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/RequestError.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum RequestError {
SecurityError(err:String, ?userMsg:String);
SecurityError(err:String, ?userMsg:String, clean:Bool);
}

14 changes: 8 additions & 6 deletions src/Server.hx
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ class Server {
}
} catch (err:RequestError) {
switch err {
case SecurityError(err, userMsg):
abort(req_t, err, 400, userMsg);
case SecurityError(err, userMsg, clean):
abort(req_t, err, 400, userMsg, clean);
}
} catch (err:Dynamic) {
abort(req_t, err, 500);
Expand All @@ -197,7 +197,7 @@ class Server {
shortId = requestId = null;
}

static function abort(req_t:Float, err:Dynamic, status:Int, ?userMessage:String)
static function abort(req_t:Float, err:Dynamic, status:Int, ?userMessage:String, clean=false)
{
try
Web.setReturnCode(status)
Expand All @@ -209,9 +209,11 @@ class Server {
trace('could not return error view anymore');
var stack = StringTools.trim(haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
trace('ERROR after ${since(req_t)} ms: $err', stack);
if (ManagedModule.cacheEnabled)
ManagedModule.uncache();
ManagedModule.callFinalizers();
if (!clean) {
if (ManagedModule.cacheEnabled)
ManagedModule.uncache();
ManagedModule.callFinalizers();
}
}

static inline function since(ref:Float)
Expand Down
10 changes: 5 additions & 5 deletions src/route/Novo.hx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class Novo {
}
#else
if (user == null)
trace('abort: user not found (${args.belNumber})'); // TODO consider switching to SecurityError
trace('abort: user not found (${args.belNumber})');
else
trace('abort: cpf does not match user (user ${user.belNumber}, cpf ${args.cpf})'); // TODO consider switching to SecurityError
trace('abort: cpf does not match user (user ${user.belNumber}, cpf ${args.cpf})');
getDefault('Consultor não encontrado ou CPF não bate');
return;
#end
Expand Down Expand Up @@ -147,7 +147,7 @@ class Novo {
show(Web.getCookies().get(CARD_COOKIE));
if (card != null)
show(Type.enumConstructor(card.state));
throw SecurityError("card request not found or in wrong state");
throw SecurityError("card request not found or in wrong state", "Você precisa começar uma nova solicitação.", true);
}
show(card.requestId);

Expand Down Expand Up @@ -223,7 +223,7 @@ class Novo {
show(Web.getCookies().get(CARD_COOKIE));
if (card != null)
show(Type.enumConstructor(card.state));
throw SecurityError("card request not found or in wrong state");
throw SecurityError("card request not found or in wrong state", "Você precisa começar uma nova solicitação.", true);
}
show(card.requestId);

Expand All @@ -244,7 +244,7 @@ class Novo {
{
var card = db.CardRequest.manager.select($requestId == key);
if (card == null)
throw SecurityError("card request not found", "Não encontramos o cartão na nossa base.");
throw SecurityError("card request not found", "Não encontramos o cartão na nossa base.", true);
show(Type.enumConstructor(card.state));

Web.setReturnCode(200);
Expand Down

0 comments on commit 635548a

Please sign in to comment.