Giter Site home page Giter Site logo

Feature: Try/Catch mechanisim about ai HOT 11 CLOSED

open-sauced avatar open-sauced commented on August 26, 2024
Feature: Try/Catch mechanisim

from ai.

Comments (11)

bdougie avatar bdougie commented on August 26, 2024

Are you seeing errors?

It would be preferred to fix this while fixing an actual error, otherwise optimization for sake of optimization is not needed at this time.

from ai.

a0m0rajab avatar a0m0rajab commented on August 26, 2024

No, I am not seeing any errors, I just noticed that when I started working on #88

from ai.

Anush008 avatar Anush008 commented on August 26, 2024

The code refactor and description generation button click handlers do have try/catch blocks implemented to handle errors generated from the API calls.

try {
if (!(await isLoggedIn())) {
return window.open(SUPABASE_LOGIN_URL, "_blank");
}
const logo = document.getElementById("ai-description-button-logo");
if (!logo) {
return;
}
const descriptionConfig = await getAIDescriptionConfig();
if (!descriptionConfig) {
return;
}
if (!descriptionConfig.enabled) {
return alert("AI PR description is disabled!");
}
logo.classList.toggle("animate-spin");
const selectedLines = document.querySelectorAll(".code-review.selected-line");
let selectedCode = Array.from(selectedLines).map(line => line.textContent)
.join("\n");
// find input with name="position" and get its value
if (!selectedCode) {
const positionElement = (commentNode.querySelector("input[name=position]")!);
const position = positionElement.getAttribute("value")!;
const codeDiv = document.querySelector(`[data-line-number="${position}"]`)?.nextSibling?.nextSibling as HTMLElement;
selectedCode = codeDiv.getElementsByClassName("blob-code-inner")[0].textContent!;
}
if (isOutOfContextBounds([selectedCode, [] ], descriptionConfig.config.maxInputLength)) {
logo.classList.toggle("animate-spin");
return alert(`Max input length exceeded. Try reducing the number of selected lines to refactor.`);
}
const token = await getAuthToken();
const suggestionStream = await generateCodeSuggestion(
token,
descriptionConfig.config.language,
descriptionConfig.config.length,
descriptionConfig.config.temperature / 10,
selectedCode,
);
logo.classList.toggle("animate-spin");
if (!suggestionStream) {
return console.error("No description was generated!");
}
const textArea = commentNode.querySelector(GITHUB_PR_SUGGESTION_TEXT_AREA_SELECTOR)!;
insertTextAtCursor(textArea as HTMLTextAreaElement, suggestionStream);
} catch (error: unknown) {
if (error instanceof Error) {
console.error("Description generation error:", error.message);
}
}

try {
if (!(await isLoggedIn())) {
return window.open(SUPABASE_LOGIN_URL, "_blank");
}
const logo = document.getElementById("ai-description-button-logo");
if (!logo) {
return;
}
const url = getPullRequestAPIURL(window.location.href);
const descriptionConfig = await getAIDescriptionConfig();
if (!descriptionConfig) {
return;
}
if (!descriptionConfig.enabled) {
return alert("AI PR description is disabled!");
}
logo.classList.toggle("animate-spin");
const [diff, commitMessages] = await getDescriptionContext(url, descriptionConfig.config.source);
if (!diff && !commitMessages) {
logo.classList.toggle("animate-spin");
return alert(`No input context was generated.`);
}
if (isOutOfContextBounds([diff, commitMessages], descriptionConfig.config.maxInputLength)) {
logo.classList.toggle("animate-spin");
return alert(`Max input length exceeded. Try setting the description source to commit-messages.`);
}
const token = await getAuthToken();
const descriptionStream = await generateDescription(
token,
descriptionConfig.config.language,
descriptionConfig.config.length,
descriptionConfig.config.temperature / 10,
descriptionConfig.config.tone,
diff,
commitMessages,
);
logo.classList.toggle("animate-spin");
if (!descriptionStream) {
return console.error("No description was generated!");
}
const textArea = document.getElementsByName(GITHUB_PR_COMMENT_TEXT_AREA_SELECTOR)[0] as HTMLTextAreaElement;
insertTextAtCursor(textArea, descriptionStream);
} catch (error: unknown) {
if (error instanceof Error) {
console.error("Description generation error:", error.message);
}
}

from ai.

a0m0rajab avatar a0m0rajab commented on August 26, 2024

The issue with warping the whole function in try/catch is that when I tried to use the functions inside this by itself, like: getDescript, description stream, I had to warp that with a try/cacth again. I think having try catch the base would be more ideal.

what your thoughts? @Anush008

from ai.

Anush008 avatar Anush008 commented on August 26, 2024

The idea is to have the caller handle the thrown errors as required.

from ai.

diivi avatar diivi commented on August 26, 2024

I think more specific errors could be handled, but we'll just log them all anyway, no other mechanism to deal with any of the for now.

So this can be done later.

from ai.

a0m0rajab avatar a0m0rajab commented on August 26, 2024

So, should we keep this open until it's done later? So that we will not forget it?

from ai.

Anush008 avatar Anush008 commented on August 26, 2024

It's just that when you call functions that throw, you can handle them as required. There's no way around it. It isn't appropriate to handle the error in the function definition since the caller might need a different behaviour on error.

It isn't something that needs to be fixed or improved.

from ai.

a0m0rajab avatar a0m0rajab commented on August 26, 2024

You can think about it as something similar to API calls, when you got 401 for example it's not throwing an error, but it's returning a response with response.ok state if it's okay then you can move forward.

Here you can check the insights project logic:

https://github.com/open-sauced/insights/blob/168312dd5521746fc28dd05871d7eb0bcfb7f655/lib/hooks/createHighlights.ts#L39-L46

from ai.

diivi avatar diivi commented on August 26, 2024

@a0m0rajab can you share an example of an error from our project? That'll help in tackling the issue too, if there is one :)

from ai.

a0m0rajab avatar a0m0rajab commented on August 26, 2024

@diivi it's more of a I could not use the util functions since that did not provide a try/catch stuff rather than an error I faced.

from ai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.