redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

Can`t get ErrorHandler to be called #3

Closed petermetz closed 10 years ago

petermetz commented 10 years ago

Hiya,

I was trying to set up an error handler and couldn't get it working. I'm not sure if it's my code or the library. Could you please have a look at my test case for which I'd expect the error handler to be called?

I'm on Dart 1.3 stable and the exact link I was testing is this: http://localhost:8080/a/b

import 'dart:io';

import 'package:bloodless/server.dart' as app;

main() {
  app.setupConsoleLog();
  app.start();
}

@app.Group('/a')
class A {

  @app.ErrorHandler(HttpStatus.INTERNAL_SERVER_ERROR)
  handleInternalServerError() {
    print('Internal server error occured!');
  }

  @app.Route('/b')
  String defaultHandler() {
    print('Executing the default handler.');

    app.abort(HttpStatus.INTERNAL_SERVER_ERROR);

    return 'This is the default response.';
  }
}
luizmineo commented 10 years ago

Hi @petermetz,

Unfortunately, bloodless doesn't support defining error handlers inside groups yet. Actually, you can have only one error handler per status code in your application, but I plan to remove this restriction in future releases.

petermetz commented 10 years ago

Hi @luizmineo,

Thanks for quick response and especially the extra info, I wasn't aware of that at all! I modified my test case by removing the group and the error handler is still not getting called. Do you know what I might be missing?


import 'dart:io';

import 'package:bloodless/server.dart' as app;

main() {
  app.setupConsoleLog();
  app.start();
}

@app.ErrorHandler(HttpStatus.INTERNAL_SERVER_ERROR)
handleInternalServerError() {
  print('Internal server error occured!');
}

@app.Route('/b')
String defaultHandler() {
  print('Executing the default handler.');

  app.abort(HttpStatus.INTERNAL_SERVER_ERROR);

  return 'This is the default response.';
}
luizmineo commented 10 years ago

Well, this is indeed a bug in bloodless :(

The error handler is being called, but the response returned by the router is overwriting the response generated by the abort() method.

As a workaround, you can just throw an exception, instead of calling abort(). I will provide a fix for this in a future release.

petermetz commented 10 years ago

I will do just like that, thank you very much again for the quick help.

I'm looking forward to those upcoming releases, the framework/library looks awesome so far, reminds me of JAX-RS.

Kind regards, Peter