Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assets: custom response when asset isn't found #3501

Closed
jonaskahn opened this issue Aug 13, 2024 · 4 comments
Closed

assets: custom response when asset isn't found #3501

jonaskahn opened this issue Aug 13, 2024 · 4 comments

Comments

@jonaskahn
Copy link

I want to make a customized handler to handle 404 error, when error happen, send back 404 page
Here is my implementation

package com.sample

import io.jooby.kt.Kooby
import io.jooby.kt.runApp
import io.jooby.netty.NettyServer
import io.jooby.OpenAPIModule
import io.jooby.StatusCode
import io.jooby.handler.AssetHandler
import io.jooby.handler.AssetSource
import java.time.Duration

class App: Kooby({
  install(NettyServer())
  install(OpenAPIModule())
  mvc(Controller())

  val www = AssetSource.create(this.classLoader, "static")
  assets(
    "/*", AssetHandler(www)
      .setMaxAge(Duration.ofDays(365))
  )

  error(StatusCode.NOT_FOUND) { ctx, _, _ ->
    ctx.sendRedirect("/404.html")
  }
})

fun main(args: Array<String>) {
  runApp(args, ::App)
}

I tried with sendRedirect or forward, but it does not work.
Could you show me the way to send a html page for 404 error?

@jknack
Copy link
Member

jknack commented Aug 13, 2024

Hi

The asset handler by default generates a silent 404 response. Try this:

assets(
    "/*", AssetHandler("404.html", www)
      .setMaxAge(Duration.ofDays(365))
  )

https://jooby.io/#static-files-spas

@jonaskahn
Copy link
Author

So, I want to set custom error for 500, 404, 403. Therefore, I seem can not apply this.
Btw, I am using SPA page, I change to default fallback to index.html, then it works like what I expected.

@jknack
Copy link
Member

jknack commented Aug 14, 2024

Got it. We can change asset handler to throw an exception instead. Meanwhile you can try this:

  1. create your own AssetSource that delegates to another asset source, so when asset is null you throw the error you want so error handler catch and handle it.

@jknack jknack changed the title error handling does not work as expected assets: custom response when asset isn't found Aug 14, 2024
@jknack jknack added this to the 3.2.9 milestone Aug 14, 2024
@jonaskahn
Copy link
Author

Got it. We can change asset handler to throw an exception instead. Meanwhile you can try this:

  1. create your own AssetSource that delegates to another asset source, so when asset is null you throw the error you want so error handler catch and handle it.

I see. I'll try that, thank you.

@jknack jknack closed this as completed in 3c3215c Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants