Skip to content

Try/catch directive or exception managementΒ #6485

@razshare

Description

@razshare

Describe the problem

In javascript the use of try/catch is sometimes overlooked because the language itself makes it easy to overlook these blocks.
When working with typescript in Svelte it's a bit hard to manage exceptions.

Take this markup and script as an example:

<button on:click={myFunction}>click me</button>
<script lang="ts">
    let someCondition = true;

    function SomeException(message) {
        this.message = message;
        this.name = 'SomeException';
    }

    /**
     * @throws SomeException
     */
    function myFunction():void{
        //some code
        if(someCondition)
            throw new SomeException("Something went wrong!")
        //some more code
    }
</script>

Clicking the button would throw an exception in the console, yes, but there's more work to be done in order to display to the user that something actually went wrong, like setting some state variable which can complicate things when you need to manage multiple types of custom exceptions.

Describe the proposed solution

Could this be treated in a way similar to the await/then directive?
Given the previous example, maybe even distinguishing between different types of exceptions/errors like so:

{#try}
   <button on:click={myFunction}>click me</button>
{:catch e1 as SomeException}
   <!--- react to some exception --->
{:catch e2 as SomeOtherException}
   <!--- react to some different exception with a different markup --->
{/try

Alternatives considered

As an alternative a dedicated dom event for catching exceptions would be nice too, like:

<button on:click={myFunction} on:exception={manageExceptions}>click me</button>

and parents would catch children exceptions if they're not already caught deeper in the code:

<div on:exception={manageExceptions}>
   <button on:click={myFunction}>click me</button>
</div>

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions