Skip to content

Commit

Permalink
Catch errors and log
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuancelin committed May 16, 2024
1 parent f48e5b8 commit 8a51249
Showing 1 changed file with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import otoroshi.storage.drivers.inmemory.S3Configuration
import otoroshi.utils.TypedMap
import otoroshi.utils.syntax.implicits._
import otoroshi.wasm._
import play.api.Logger
import play.api.libs.json._
import play.api.libs.ws.DefaultWSCookie
import play.api.mvc._
Expand Down Expand Up @@ -126,6 +127,8 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
override def description: Option[String] = "Dynamically run Js Modules without the need to compile them before".some
override def defaultConfigObject: Option[NgPluginConfig] = JsModulePluginConfig.default.some

private val logger = Logger("JsModulePlugin")

override def noJsForm: Boolean = true

override def jsonDescription(): JsObject = Json.obj(
Expand Down Expand Up @@ -330,17 +333,29 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
modulesCache.put(path, path)
path.vfuture
} else if (path.startsWith("s3://")) {
logger.info(s"fetching from S3: ${path}")
val config = S3Configuration.format.reads(JsObject(pluginConfig.headers.mapValues(_.json))).get
fileContent(pluginConfig.module.replaceFirst("s3://", ""), config)(env.otoroshiExecutionContext, env.otoroshiMaterializer).flatMap {
case None => getDefaultCode(pluginConfig).map { code =>
modulesCache.put(pluginConfig.module, code)
code
case None => {
logger.info(s"unable to fetch from S3: ${path}")
getDefaultCode(pluginConfig).map { code =>
modulesCache.put(pluginConfig.module, code)
code
}
}
case Some((_, codeRaw)) => {
val code = codeRaw.utf8String
modulesCache.put(path, code)
code.vfuture
}
}.recoverWith {
case t: Throwable => {
logger.error(s"error when fetch from S3: ${path}", t)
getDefaultCode(pluginConfig).map { code =>
modulesCache.put(pluginConfig.module, code)
code
}
}
}
} else {
getDefaultCode(pluginConfig).map { code =>
Expand Down Expand Up @@ -403,6 +418,10 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
}
}
})
}.recover {
case t: Throwable =>
logger.error("plugin error on access phase", t)
NgAccess.NgDenied(Results.InternalServerError(Json.obj("error" -> "plugin error on access phase")))
}
}

Expand Down Expand Up @@ -454,6 +473,10 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
}
}
})
}.recover {
case t: Throwable =>
logger.error("plugin error on backend call phase", t)
Left(NgProxyEngineError.NgResultProxyEngineError(Results.InternalServerError(Json.obj("error" -> "plugin error on backend call phase"))))
}
}

Expand Down Expand Up @@ -516,6 +539,10 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
}
}
})
}.recover {
case t: Throwable =>
logger.error("plugin error on transform request phase", t)
Left(Results.InternalServerError(Json.obj("error" -> "plugin error on transform request phase")))
}
}

Expand Down Expand Up @@ -576,6 +603,10 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
}
}
})
}.recover {
case t: Throwable =>
logger.error("plugin error on transform response phase", t)
Left(Results.InternalServerError(Json.obj("error" -> "plugin error on transform response phase")))
}
}

Expand Down Expand Up @@ -626,5 +657,9 @@ class JsModulePlugin extends NgAccessValidator with NgRequestTransformer with Ng
}
})
}
}.recover {
case t: Throwable =>
logger.error("plugin error on transform error phase", t)
NgPluginHttpResponse.fromResult(Results.InternalServerError(Json.obj("error" -> "plugin error on transform error phase")))
}
}

0 comments on commit 8a51249

Please sign in to comment.