Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

REST Countries API w/ Svelkit, Ts, Tailwind, Axios & SSR prefetching

#svelte#tailwind-css#typescript#axios
AntoineC 1,180

@AntoineC-dev

Desktop design screenshot for the REST Countries API with color theme switcher coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
  • API
4advanced
View challenge

Design comparison


SolutionDesign

Solution retrospective


It was a great project to practice dynamic routes in Sveltkit.

But i could not find the way to catch the API call errors on the client side.

Maybe you know how ? 😊

I would also like to implement a caching system (maybe service worker ??) for already fetched endpoints.

If you have any advice or resources on that matter or anything about the layout or the code feel free to share.

Have a nice day/night. Peace

Community feedback

@fadhilradh

Posted

Hi Antoinec,

Your project is good and works as expected.

Regarding your question about : But i could not find the way to catch the API call errors on the client side.

I read your code, in the requests.config.ts, you need to pass error as catch parameter, then pass the error again in your throw.

So, insted of writing this in your code :

 catch (_) {
    throw new Error("Sorry! We could not access the API.");
  }

You should write like so :

 catch (error) {
    throw new Error(`Sorry! We could not access the API because ${error}`);
  }

Hope it helps and good luck!

1

AntoineC 1,180

@AntoineC-dev

Posted

@fadhilradh Hey thank you for your answer.

I might have use poor phrasing in my comment.

What i meant is that the Sveltekit shadow endpoints does catch the error i throw whose content is not the important part for me. But I was having troubles understanding where to format and display the error correctly.

But searching again on the docs i found the answer. There is a layout type page __error.svelte that you shall create and display whatever the error is there

Here is the link for whoever find it usefull:

Sveltkit Error Page Layout

now i can catch the thrown error on the page and display it as i want like so:

in the script type ="module" tag:

  import type { Load } from "@sveltejs/kit";
  export const load: Load = async ({ error }) => {
    return {
      props: {
        error: error?.message,
      },
    };
  };

In the normal script tag:

export let error: string;

In the html:

<div>
// Show the error here as i like
</div>

Full code here

Thank you for the time you took to help me it is really appreciated.

Happy coding 👍

1

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord