Mounayer / OptimizeIt

OptimizeIt is a tool that is meant to help you Optimize your code. You simply run this command-line application and give it a source code file name which it then optimizes for peak performance and readability.
MIT License
7 stars 3 forks source link

Make OptimizeIt exit with appropriate error codes and appropriate error messages in all cases #14

Closed Mounayer closed 1 month ago

Mounayer commented 1 month ago

Description

While this exists in some cases in OptimizeIt, it isn't implemented everywhere which isn't consistent. OptimizeIt should exist with appropriate error codes, i.e.: 0 if no errors occur, none 0 if an error occurs.

Example, in file_writer, proper exit code and exit message exist:

/**
 * Writes the data to a file in the output directory.
 *
 * @param { string } data - The data to write to the file.
 * @param { string } outputFile - The name of the output file.
 */
function fileWriter(data: string, outputFile: string): void {
  try {
    if (data.startsWith('```')) {
      const dataParts = data.split('\n');
      dataParts.shift();
      data = dataParts.join('\n');
    }

    data = data.replace(/```$/, '').trim();

    const outputDir = path.resolve(__dirname, '../../output');

    console.log('outputDir', outputDir);
    if (!fs.existsSync(outputDir)) {
      fs.mkdirSync(outputDir, { recursive: true });
    }

    const outputPath = path.join(outputDir, outputFile);
    fs.writeFileSync(outputPath, data);
  } catch (err) {
    console.error(`Error writing to file: ${err}`);
    process.exit(1);
  }
}

However, in groq, there does not exist any specific error code for when the request fails for any reason:

  /**
   * Run the GroqChat instance.
   *
   * @param fileName - The name of the file.
   * @param context - The context of the code.
   * @param model - The model to use for the chat completion.
   * @param temperature - The temperature for the chat completion.
   * @returns { Promise<string | undefined> } The chat data from the Groq API.
   */
  public async run(
    fileName: string,
    context: string,
    model: string,
    temperature: number,
    tokenUsageInformation: boolean,
  ): Promise<string | undefined> {
    try {
      const chatCompletion = await this.getGroqChatCompletion(
        fileName,
        context,
        model,
        temperature,
      );

      if (tokenUsageInformation) {
        if (!chatCompletion.usage) {
          throw new Error('Token usage information is not available');
        }
        this.accumulateToken(chatCompletion?.usage);
      }

      const chatData = chatCompletion.choices[0]?.message?.content || '';
      console.log(chatData);

      return chatData;
    } catch (err: any) {
      if (err.error && err.error.error && typeof err.error.error === 'object') {
        const errorMessage = err.error.error.message;
        const errorType = err.error.error.type;

        console.error(
          `Error Message: ${errorMessage}, Error Type: ${errorType}`,
        );
      } else {
        console.error('An unexpected error occurred:', err);
      }
    }
  }

In this case, the run function should exit with code 1, which indicates general failure or an error condition.

Mounayer commented 1 month ago

Closed by https://github.com/Mounayer/OptimizeIt/commit/679a7b146aebda4b4512aae21a451744f8107012