Files
documentdb/scripts/generate_error_values.sh
T
Vinod Sridharan 52e932125d Merged PR 1446548: [Error|OSS] New Error Format & Usage
### Does this PR have any customer impact?
No

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Refactor

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No(ish)

### Are you introducing any new config? If yes, do you have tests with and without them being set?
No
### Release Note (Refer [Template](../docs/release_notes/pgmongo_template.md))

### Description
Start using the new Helio error codes. These are of the format M0000->M0ZZZ
Convert helio_core to use M0001 for BadValue

With the new error syntax, the errmsg is the user facing error message.
Any PII Safe messages intended for the server will now be placed under errdetail_log (rather than errhint).

----
#### AI description  (iteration 1)
#### PR Classification
New feature: Introduces a new error format and usage.

#### PR Summary
This pull request implements a new error format and updates the usage of error handling across multiple components.
- `logger_hooks.c`: Adds logic to transform client log data and ensure error details are properly set.
- `PostgresMongoResultExtensions.cs`: Refactors error handling to include sanitized error hints and details.
- `error.rs` and `pg.rs`: Adds support for known PostgreSQL error codes and refines error handling.
- `PostgresError.cs` and `PostgresClient.cs`: Updates error properties to include error hints and details.
- Multiple files in `pg_helio_core`: Replaces `ERRCODE_INVALID_TEXT_REPRESENTATION` with `ERRCODE_HELIO_BADVALUE` for better error categorization.
2024-09-05 18:18:23 +03:00

182 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
# fail if trying to reference a variable that is not set.
set -u
# exit immediately if a command exits with a non-zero status
set -e
sourceFile=$1
filePathDest=$2
errorNamesPath=$3
filePathName=$(basename $filePathDest)
filePathName="${RANDOM}-${filePathName}"
filePath="/tmp/$filePathName"
echo "Writing header for $filePath"
cat << EOF > $filePath
/*-------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* include/utils/helio_errors.h
*
* Utilities for Helio Error Definition.
* This file is generated - please modify the source (mongoerrors.csv)
*
*-------------------------------------------------------------------------
*/
#ifndef HELIO_ERRORS_H
#define HELIO_ERRORS_H
#include <utils/elog.h>
EOF
echo "ErrorName,ErrorCode,MongoError" > $errorNamesPath
# Postgres Printable Error Range
ValidChars=(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
isFirst=""
baseLetter="M"
baseSecond="0"
baseThird="0"
baseFourth="0"
baseFifth="0"
function IncrementCharacter()
{
local _character=$1
if [[ $_character -lt 35 ]]; then
_character=$((++_character))
echo $_character
else
echo "-1"
fi
}
function IncrementErrorCode()
{
newFifth=$(IncrementCharacter $baseFifth)
if [[ "$newFifth" == "-1" ]]; then
baseFifth="0"
newFourth=$(IncrementCharacter $baseFourth)
if [[ "$newFourth" == "" ]]; then
baseFourth="0"
newThird=$(IncrementCharacter $baseThird)
if [[ "$newThird" == "" ]]; then
baseThird="0"
newSecond=$(IncrementCharacter $baseSecond)
if [[ "$newSecond" == "" ]]; then
echo "Max error is reached."
exit 1
else
baseSecond=$newSecond
return;
fi
else
baseThird=$newThird;
return;
fi
else
baseFourth=$newFourth
return;
fi
else
baseFifth=$newFifth
return;
fi
}
declare -A myKeys=()
declare -a myIndexes=()
isFirst=""
# Read through the file and get the error codes
for fileLine in $(cat $sourceFile); do
if [ "$isFirst" = "" ]; then
isFirst="false"
continue;
fi
regex="([A-Za-z0-9]+),([0-9]+),([0-9]+)"
if [[ $fileLine =~ $regex ]]; then
_name="${BASH_REMATCH[1]}"
_code="${BASH_REMATCH[2]}"
_ordinal="${BASH_REMATCH[3]}"
lineToEnter="${_name},${_code}"
# Ensure uniqueness of ordinal values.
if [[ ${myKeys["$_ordinal"]:-''} != '' ]]; then
echo "Error Ordinal Used Already $_ordinal"
exit 1;
fi
myKeys["$_ordinal"]=$lineToEnter
myIndexes+=($_ordinal)
else
echo "$fileLine doesn't match";
exit 1
fi
done
# Sort by ordinal
sorted=($(sort <<<"${myIndexes[*]}"))
prevTrackedIndex=0
for fileIndex in $( echo ${sorted[@]} ); do
prevOrdinal=$(( fileIndex - 1 ))
if [[ "$prevOrdinal" != "$prevTrackedIndex" ]]; then
echo "There's a gap in the ordinal index. This is not allowed: Index $fileIndex, Previous $prevTrackedIndex"
exit 1;
fi
prevTrackedIndex=$fileIndex
fileLine=${myKeys[$fileIndex]}
regex="([A-Za-z0-9]+),([0-9]+)"
if [[ $fileLine =~ $regex ]]; then
name="${BASH_REMATCH[1]}"
code="${BASH_REMATCH[2]}"
else
echo "$fileLine doesn't match";
exit 1
fi
errorNameUpper=${name^^}
secondChar=${ValidChars[$baseSecond]}
thirdChar=${ValidChars[$baseThird]}
fourthChar=${ValidChars[$baseFourth]}
fifthChar=${ValidChars[$baseFifth]}
lineToWrite="#define ERRCODE_HELIO_$errorNameUpper MAKE_SQLSTATE('$baseLetter', '$secondChar', '$thirdChar', '$fourthChar', '$fifthChar')"
if (( ${#lineToWrite} > 89 )); then
# make citus indent happy
echo "#define ERRCODE_HELIO_$errorNameUpper \\" >> $filePath
echo " MAKE_SQLSTATE('$baseLetter', '$secondChar', '$thirdChar', '$fourthChar', '$fifthChar')" >> $filePath
else
echo "#define ERRCODE_HELIO_$errorNameUpper MAKE_SQLSTATE('$baseLetter', '$secondChar', '$thirdChar', '$fourthChar', '$fifthChar')" >> $filePath
fi
echo "#define MONGO_CODE_${errorNameUpper} $code" >> $filePath
echo >> $filePath
echo "${name},${baseLetter}${secondChar}${thirdChar}${fourthChar}${fifthChar},${code}" >> $errorNamesPath
IncrementErrorCode
done
echo "Writing footer for $filePath"
echo "#endif" >> $filePath
echo "Moving $filePath to $filePathDest"
mv $filePath $filePathDest