<commit_before>﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace iMobileDevice.Generator
{
    internal class ErrorExtensionExtractor
    {
        private ModuleGenerator generator;
        private FunctionVisitor visitor;

        public ErrorExtensionExtractor(ModuleGenerator generator, FunctionVisitor visitor)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }

            this.generator = generator;
            this.visitor = visitor;
        }

        public void Generate()
        {
            var errorEnum = this.generator.Types.SingleOrDefault(t => t.Name.EndsWith("Error"));

            if (errorEnum == null)
            {
                return;
            }

            // Add the exception type

            // Generate the {Name}Exception class
            CodeTypeDeclaration exceptionType = new CodeTypeDeclaration();
            exceptionType.Name = $"{this.generator.Name}Exception";
            exceptionType.BaseTypes.Add(typeof(Exception));
            exceptionType.Comments.Add(
                new CodeCommentStatement(
                    $"Represents an exception that occurred when interacting with the {this.generator.Name} API.",
                    true));
            exceptionType.CustomAttributes.Add(
                new CodeAttributeDeclaration(
                    new CodeTypeReference(
                        typeof(SerializableAttribute))));

            var defaultConstrutor = new CodeConstructor();
            defaultConstrutor.Attributes = MemberAttributes.Public;
            defaultConstrutor.Comments.Add(
                new CodeCommentStatement(
                    $"<summary>\r\n Initializes a new instance of the <see cref=\"{exceptionType.Name}\"/> class.\r\n </summary>",
                    true));
            exceptionType.Members.Add(defaultConstrutor);

            var errorConstructor = new CodeConstructor();
            errorConstructor.Attributes = MemberAttributes.Public;
            errorConstructor.Comments.Add(
                new CodeCommentStatement(
                    $"<summary>\r\n Initializes a new instance of the <see cref=\"{exceptionType.Name}\"/> class with a specified error code.\r\n <summary>",
                    true));
            errorConstructor.Comments.Add(
                new CodeCommentStatement(
                    $"<param name=\"error\">\r\n The error code of the error that occurred.\r\n </param>",
                    true));
            errorConstructor.BaseConstructorArgs.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeTypeReferenceExpression(typeof(string)),
                        "Format"),
                    new CodePrimitiveExpression($"An {this.generator.Name} error occurred. The error code was {0}"),<commit_msg>Fix error where exception messages were not being formatted correctly.<commit_after>﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace iMobileDevice.Generator
{
    internal class ErrorExtensionExtractor
    {
        private ModuleGenerator generator;
        private FunctionVisitor visitor;

        public ErrorExtensionExtractor(ModuleGenerator generator, FunctionVisitor visitor)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }

            this.generator = generator;
            this.visitor = visitor;
        }

        public void Generate()
        {
            var errorEnum = this.generator.Types.SingleOrDefault(t => t.Name.EndsWith("Error"));

            if (errorEnum == null)
            {
                return;
            }

            // Add the exception type

            // Generate the {Name}Exception class
            CodeTypeDeclaration exceptionType = new CodeTypeDeclaration();
            exceptionType.Name = $"{this.generator.Name}Exception";
            exceptionType.BaseTypes.Add(typeof(Exception));
            exceptionType.Comments.Add(
                new CodeCommentStatement(
                    $"Represents an exception that occurred when interacting with the {this.generator.Name} API.",
                    true));
            exceptionType.CustomAttributes.Add(
                new CodeAttributeDeclaration(
                    new CodeTypeReference(
                        typeof(SerializableAttribute))));

            var defaultConstrutor = new CodeConstructor();
            defaultConstrutor.Attributes = MemberAttributes.Public;
            defaultConstrutor.Comments.Add(
                new CodeCommentStatement(
                    $"<summary>\r\n Initializes a new instance of the <see cref=\"{exceptionType.Name}\"/> class.\r\n </summary>",
                    true));
            exceptionType.Members.Add(defaultConstrutor);

            var errorConstructor = new CodeConstructor();
            errorConstructor.Attributes = MemberAttributes.Public;
            errorConstructor.Comments.Add(
                new CodeCommentStatement(
                    $"<summary>\r\n Initializes a new instance of the <see cref=\"{exceptionType.Name}\"/> class with a specified error code.\r\n <summary>",
                    true));
            errorConstructor.Comments.Add(
                new CodeCommentStatement(
                    $"<param name=\"error\">\r\n The error code of the error that occurred.\r\n </param>",
                    true));
            errorConstructor.BaseConstructorArgs.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeTypeReferenceExpression(typeof(string)),
                        "Format"),
                    new CodePrimitiveExpression($"An {this.generator.Name} error occurred. The error code was {{0}}"),
{'fraction_non_alphanumeric': 0.08549501151189562, 'fraction_numerical': 0.0003069838833461243, 'mean_word_length': 1.8793636765355723, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6433503', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1397, 'n_words': 360}