Skip to content

Instantly share code, notes, and snippets.

@zeryx
Created October 29, 2019 19:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zeryx/d2b9a5d548fe313e49513c87f9b49bd0 to your computer and use it in GitHub Desktop.
package com.algorithmia.handler
import java.io.FileOutputStream
import java.io.PrintStream
import scala.util.Try
case class ResponseHandler(){
val FIFOPATH = "/tmp/algoout"
private def write(data: String): Try[Unit] = {
Try(new PrintStream(new FileOutputStream(this.FIFOPATH, true)))
.map({s => s.println(data); s})
.map({s => s.flush(); s})
.map(_.close())
}
def writeErrorToPipe(e: Throwable): Try[Unit] = {
val serializable = SerializableException.fromException(e)
write(serializable.serialize()) """no implicit definition of Writes[SerializableException]"""
}
}
package com.algorithmia.handler
import org.apache.commons.lang3.exception.ExceptionUtils
import play.api.libs.json._
case class SerializableException(message: String, stacktrace: String, error_type: String) {
def serialize()(implicit s: Writes[SerializableException]): String = {
Json.toJson(this).toString()
}
}
object SerializableException {
def fromException(e: Throwable): SerializableException = {
val stackTrace = ExceptionUtils.getStackTrace(e)
val message = ExceptionUtils.getMessage(e)
val errorType = e.getClass.toString
SerializableException(message, stackTrace, errorType)
}
implicit def writes: (SerializableException => JsValue) => Writes[SerializableException] = Writes[SerializableException]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment