type APIErrorMessage = {
    status: number,
    responseText: string
}
class APIError extends Error{
    declare info:APIErrorMessage
    constructor(message:APIErrorMessage) {
        super(JSON.stringify(message))
        this.info = message;
        this.name = "AuthError"; // (different names for different built-in error classes)
    }
}

const attemptAPIAction = async (action:Function,request:Request) => {
    try {
        return await action(request);
    }
    catch (e) {
        if (e instanceof APIError) {
            return new Response(e.info.responseText, { status: e.info.status });
        }
        else {
            throw e;
        }
    }
}

export { APIError, attemptAPIAction }