<commit_before>package org.jetbrains.kotlinx.jupyter.exceptions

import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptDiagnostic

class ReplCompilerException(
    val failedCode: String,
    val errorResult: ResultWithDiagnostics.Failure? = null,
    message: String? = null
) :
    ReplException(message ?: errorResult?.getErrors() ?: "") {

    val firstError = errorResult?.reports?.firstOrNull {
        it.severity == ScriptDiagnostic.Severity.ERROR || it.severity == ScriptDiagnostic.Severity.FATAL
    }

    override fun getAdditionalInfoJson(): JsonObject? {
        return firstError?.location?.let {
            val errorMessage = firstError.message
            JsonObject(
                mapOf(
                    "lineStart" to JsonPrimitive(it.start.line),
                    "colStart" to JsonPrimitive(it.start.col),
                    "lineEnd" to JsonPrimitive(it.end?.line ?: -1),
                    "colEnd" to JsonPrimitive(it.end?.col ?: -1),
                    "message" to JsonPrimitive(errorMessage),
                    "path" to JsonPrimitive(firstError.sourcePath.orEmpty())
                )
            )
        }
    }

    override fun render() = message
}

fun <T> ResultWithDiagnostics<T>.getErrors(): String {
    val filteredReports = reports.filter {
        it.code != ScriptDiagnostic.incompleteCode
    }
<commit_msg>Store cause of REPL compiler exception<commit_after>package org.jetbrains.kotlinx.jupyter.exceptions

import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptDiagnostic

class ReplCompilerException(
    val failedCode: String,
    val errorResult: ResultWithDiagnostics.Failure? = null,
    message: String? = null
) :
    ReplException(
        message ?: errorResult?.getErrors() ?: "",
        errorResult?.reports?.map { it.exception }?.firstOrNull()
    ) {

    val firstError = errorResult?.reports?.firstOrNull {
        it.severity == ScriptDiagnostic.Severity.ERROR || it.severity == ScriptDiagnostic.Severity.FATAL
    }

    override fun getAdditionalInfoJson(): JsonObject? {
        return firstError?.location?.let {
            val errorMessage = firstError.message
            JsonObject(
                mapOf(
                    "lineStart" to JsonPrimitive(it.start.line),
                    "colStart" to JsonPrimitive(it.start.col),
                    "lineEnd" to JsonPrimitive(it.end?.line ?: -1),
                    "colEnd" to JsonPrimitive(it.end?.col ?: -1),
                    "message" to JsonPrimitive(errorMessage),
                    "path" to JsonPrimitive(firstError.sourcePath.orEmpty())
                )
            )
        }
    }

    override fun render() = message
}

fun <T> ResultWithDiagnostics<T>.getErrors(): String {
    val filteredReports = reports.filter {
        it.code != ScriptDiagnostic.incompleteCode
    }

{'fraction_non_alphanumeric': 0.10249839846252402, 'fraction_numerical': 0.0012812299807815502, 'mean_word_length': 2.5488636363636363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3146940', 'n_tokens_mistral': 885, 'n_tokens_neox': 865, 'n_words': 159}