Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
Java API - cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
teamon committed Nov 5, 2012
1 parent 21f4d88 commit 61afdb4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Add `play-airbrake` to your `project/Build.scala` file

``` scala
val appDependencies = Seq(
"eu.teamon" %% "play-airbrake" % "0.2.0"
"eu.teamon" %% "play-airbrake" % "0.2.1"
)

val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
Expand Down Expand Up @@ -45,14 +45,12 @@ For javascript notifications
For java integration your app/Global.java should look like this

```java
@Override
public Result onError(RequestHeader request, Throwable t) {
Map<String, String> data = new HashMap<String, String>();
for (Entry<String, String[]> value : request.headers().entrySet()) {
data.put(value.getKey(), Arrays.deepToString(value.getValue()));
}
Airbrake.notify(request.method(), request.uri(), data, t);
return super.onError(request, t);
class Global extends GlobalSettings {
@Override
public Result onError(RequestHeader request, Throwable t) {
Airbrake.notify(request, t);
return super.onError(request, t);
}
}
```

Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ organization := "eu.teamon"

name := "play-airbrake"

version := "0.2.0"
version := "0.2.1"

scalaVersion := "2.9.1"

Expand All @@ -15,7 +15,7 @@ resolvers ++= Seq(
)

libraryDependencies ++= Seq(
"play" %% "play" % Option(System.getenv("PLAY_VERSION")).getOrElse("2.0") % "compile"
"play" %% "play" % Option(System.getenv("PLAY_VERSION")).getOrElse("2.0.4") % "compile"
)

seq(scalajarsSettings:_*)
Expand Down
50 changes: 34 additions & 16 deletions src/main/scala/play/airbrake/Airbrake.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,44 @@ object Airbrake {
private lazy val apiKey = app.configuration.getString("airbrake.apiKey") getOrElse { throw PlayException("Configuration error", "Could not find airbrake.apiKey in settings") }
private lazy val ssl = app.configuration.getBoolean("airbrake.ssl").getOrElse(false)


def notify(request: RequestHeader, th: Throwable) = if(enabled){
Akka.future {
val scheme = if(ssl) "https" else "http"
WS.url(scheme + "://api.airbrake.io/notifier_api/v2/notices").post(formatNotice(app, apiKey, request.method, request.uri, request.session.data, liftThrowable(th))).onRedeem { response =>
Logger.info("Exception notice sent to Airbrake")
}
}
/**
* Scala API
*
* {{{
* // app/Global.scala
* override def onError(request: RequestHeader, ex: Throwable) = {
* Airbrake.notify(request, ex)
* super.onError(request, ex)
* }
* }}}
*/
def notify(request: RequestHeader, th: Throwable): Unit = if(enabled) _notify(request.method, request.uri, request.session.data, th)

/**
* Java API
*
* {{{
* // app/Global.java
* @Override
* public Result onError(RequestHeader request, Throwable t) {
* Airbrake.notify(request, t);
* return super.onError(request, t);
* }
* }}}
*/
def notify(request: play.mvc.Http.RequestHeader, th: Throwable): Unit = if(enabled){
val data = request.headers.toMap.mapValues(_.toString)
_notify(request.method, request.uri, data, th)
}

def notify(method:String, uri:String, data:java.util.Map[String,String], th: Throwable) = if(enabled){

val myMap = scala.collection.JavaConversions.mapAsScalaMap(data).toMap

protected def _notify(method: String, uri: String, data: Map[String, String], th: Throwable): Unit =
Akka.future {
val scheme = if(ssl) "https" else "http"
WS.url(scheme + "://api.airbrake.io/notifier_api/v2/notices").post(formatNotice(app, apiKey, method, uri, myMap, liftThrowable(th))).onRedeem { response =>
WS.url(scheme + "://api.airbrake.io/notifier_api/v2/notices").post(formatNotice(app, apiKey, method, uri, data, liftThrowable(th))).onRedeem { response =>
Logger.info("Exception notice sent to Airbrake")
}
}
}


def js = if(enabled) { """
<script src="http://cdn.airbrake.io/notifier.min.js"></script>
Expand All @@ -50,7 +68,7 @@ object Airbrake {
case e: PlayException.UsefulException => e
case e => UnexpectedException(unexpected = Some(e))
}

protected def formatNotice(app: Application, apiKey: String, method: String, uri: String, data: Map[String,String], ex: UsefulException) = {
<notice version="2.2">
<api-key>{ apiKey }</api-key>
Expand Down Expand Up @@ -96,5 +114,5 @@ object Airbrake {
val line = "%s.%s(%s)" format (e.getClassName, e.getMethodName, e.getFileName)
<line file={line} number={e.getLineNumber.toString}/>
}

}

0 comments on commit 61afdb4

Please sign in to comment.