Skip to content

Commit

Permalink
Exception handling and JSONResponse.
Browse files Browse the repository at this point in the history
  • Loading branch information
daegalus committed Jun 28, 2013
1 parent 52af4e2 commit 12a41fd
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.1.7
- Fixed bug where if there is an exception in the handler, it will crash the server. It now should not do that.
- Added a new jsonResponse function to the context. When passed a JSONifiable object/map it will JSONify it and send it with appropriate contenttype headers.

v0.1.6
- Changed static file handling to use the wonderful [library](https://github.com/DanieleSalatti/static-file-handler) created by Daniele Salatti.
- Added functions to be able to add MimeTypes to the StaticFileHandler library from Fukiya.
Expand Down
3 changes: 2 additions & 1 deletion lib/fukiya.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:json' as JSON;
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:formler/formler.dart';
import 'package:logging/logging.dart';
import 'package:static_file_handler/static_file_handler.dart';
part 'fukiya_request_handler.dart';
part 'fukiya_router.dart';
Expand Down Expand Up @@ -72,7 +73,7 @@ class Fukiya {
_router.staticFileHandler = new StaticFileHandler(basePath);
}

/**
/**
* Adds additional mime types to the static file handler's mime types list in addition to the existing ones.
*/
void addMimeType(String extension, String mimeType) {
Expand Down
10 changes: 10 additions & 0 deletions lib/fukiya_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class FukiyaContext {
response.close();
}

/**
* Sends the string as JSON output and closes the response.
*/
void jsonResponse(output) {
response.headers.contentType = new ContentType("application", "json", charset: "utf-8");
response.write(JSON.stringify(output));
response.done.catchError((e) => print("Error sending response ${e}"));
response.close();
}

/**
* Sends a 302 Moved Temporarily redirect as a response to the URL provided.
*/
Expand Down
8 changes: 7 additions & 1 deletion lib/fukiya_request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class FukiyaRequestHandler {
}

void _handle(FukiyaContext context) {
_handler(context);
try {
_handler(context);
} catch (error) {
//TODO: Change these to use a Logger once I figure out how to get a Logger to always print, even in tests.
print("[Fukiya][Error] There was an error handling the request with handler for $method $path.");
print("[Fukiya][Error] $error");
}
}
}
8 changes: 0 additions & 8 deletions lib/fukiya_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ class FukiyaRouter {

FukiyaRequestHandler finalRoute = prioritizeRouter(context, filteredRoutes);

/* if (finalRoute != null) {
finalRoute._handle(context);
} else if (useStaticFileHandling && context.request.method == "GET") {
staticFileHandler.handleRequest(httpRequest);
} else {
context.response.statusCode = HttpStatus.NOT_FOUND;
context.response.close();
}*/
if (useStaticFileHandling && context.request.method == "GET") {
var file = new File(staticFilePath + context.request.uri.path);
file.exists().then((exists) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fukiya
version: 0.1.6
version: 0.1.7
author: Yulian Kuncheff <yulian@kuncheff.com>
description: A simple framework to improve and simplify the creation of server-side dart applications.
homepage: https://github.com/Daegalus/fukiya
Expand Down
1 change: 1 addition & 0 deletions test/fukiya_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void main() {

void getHandler(FukiyaContext context) {
context.send("GET OK");
throw new Exception("This is a successful test exception catch for GET /");
}

void putHandler(FukiyaContext context) {
Expand Down

0 comments on commit 12a41fd

Please sign in to comment.