SvelteKit UnhandledPromiseRejection 出现奇怪的 TLS 连接错误

发布于 2025-01-20 23:44:05 字数 2229 浏览 1 评论 0原文

我在这里有一个奇怪的错误集合,我在互联网上找不到任何解决方案(Maby我对Google很糟糕)。

我创建了一个网站,其中包含以下文件/文件夹树:

“

使用此设置,我首先收到此错误消息:

客户网络套接字在建立安全TLS连接之前断开连接。

我找不到其他解决方案,然后重新加载页面。 在决定自己处理错误之后,我尝试了以下操作:

api.js

import axios from 'axios';
import axiosRetry from 'axios-retry';

const base = process.env['API_ENDPOINT'];
axiosRetry(axios, { retries: 5 });

async function send({ path }) {
    let pages = 0;
    let resp;

    try {
        resp = await axios.get(`${base}/${path}`);
    } catch (error) {
        throw error;
    }

    if ('X-WP-TotalPages' in resp.headers) {
        pages = resp.headers['X-WP-TotalPages'];
    }

    return {
        pages: pages,
        body: resp.data
    };
}

export async function get(path) {
    return send({ method: 'GET', path });
}

和我称其为landing.js:

import { writable } from 'svelte/store';
import { browser } from '$app/env';

import * as api from '$lib/api';

let loading = false;
let posts = [];

const list = writable({
    loading,
    posts
});

export default {
    subscribe: list.subscribe,
    async fetchNews() {
        if (loading) return {};
        loading = true;

        list.set({ loading, posts });

        let lang = 'de';

        if (browser) {
            lang = localStorage.getItem('lang') || 'de';
        }

        try {
        const res = await api.get(`posts?filter[lang]=${lang}&per_page=4&_embed`);
        } catch (error) {
            throw error;
        }

        posts = await res.body;

        posts.map((post) => {
            if (post._embedded['wp:featuredmedia'])
                post.image = post._embedded['wp:featuredmedia'][0].source_url;
            else post.image = '/news/news_placeholder.png';
        });

        loading = false;

        list.set({ loading, posts });
    }
};

现在新错误就是这样:

[毫无疑问:此错误是通过投掷而引发的 在没有捕获块的异步功能的内部或通过拒绝 未处理.catch()的承诺。承诺被拒绝 原因“测试”。

也许我只是一个白痴,但是我在这里确实需要一些帮助!

事先感谢您。

I have here a strange collection of error, for which i canno't find any solutions on the internet ( maby i'm just bad with google ).

I created a website, with the following files/folder tree:

File tree

With this setup i first got this error message:

Client network socket disconnected before secure TLS connection was established.

Which I didn't find any solution for other then reload the page.
After deciding handling the error myself i tried this:

api.js

import axios from 'axios';
import axiosRetry from 'axios-retry';

const base = process.env['API_ENDPOINT'];
axiosRetry(axios, { retries: 5 });

async function send({ path }) {
    let pages = 0;
    let resp;

    try {
        resp = await axios.get(`${base}/${path}`);
    } catch (error) {
        throw error;
    }

    if ('X-WP-TotalPages' in resp.headers) {
        pages = resp.headers['X-WP-TotalPages'];
    }

    return {
        pages: pages,
        body: resp.data
    };
}

export async function get(path) {
    return send({ method: 'GET', path });
}

And i call it in (for example) landing.js:

import { writable } from 'svelte/store';
import { browser } from '$app/env';

import * as api from '$lib/api';

let loading = false;
let posts = [];

const list = writable({
    loading,
    posts
});

export default {
    subscribe: list.subscribe,
    async fetchNews() {
        if (loading) return {};
        loading = true;

        list.set({ loading, posts });

        let lang = 'de';

        if (browser) {
            lang = localStorage.getItem('lang') || 'de';
        }

        try {
        const res = await api.get(`posts?filter[lang]=${lang}&per_page=4&_embed`);
        } catch (error) {
            throw error;
        }

        posts = await res.body;

        posts.map((post) => {
            if (post._embedded['wp:featuredmedia'])
                post.image = post._embedded['wp:featuredmedia'][0].source_url;
            else post.image = '/news/news_placeholder.png';
        });

        loading = false;

        list.set({ loading, posts });
    }
};

Now the new error is something like this:

[UnhandledPromiseRejection: This error originated either by throwing
inside of an async function without a catch block, or by rejecting a
promise which was not handled with .catch(). The promise rejected with
the reason "Test".] { code: 'ERR_UNHANDLED_REJECTION' }

Maybe I'm just an idiot right, but i need really some help here!

Thanks in advance for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2025-01-27 23:44:05

尝试将整个函数体包装在 try {...} 中,而不是 const res = wait api.get... 行,并观察错误发生的具体位置。

Try wrapping the whole function body in try {...}, instead of the const res = await api.get... line and observe where exactly the error happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文