directus

Форк
0
117 строк · 2.6 Кб
1
import {
2
	ContainsNullValuesError,
3
	InvalidForeignKeyError,
4
	NotNullViolationError,
5
	RecordNotUniqueError,
6
	ValueOutOfRangeError,
7
	ValueTooLongError,
8
} from '@directus/errors';
9
import type { PostgresError } from './types.js';
10

11
enum PostgresErrorCodes {
12
	FOREIGN_KEY_VIOLATION = '23503',
13
	NOT_NULL_VIOLATION = '23502',
14
	NUMERIC_VALUE_OUT_OF_RANGE = '22003',
15
	UNIQUE_VIOLATION = '23505',
16
	VALUE_LIMIT_VIOLATION = '22001',
17
}
18

19
export function extractError(error: PostgresError): PostgresError | Error {
20
	switch (error.code) {
21
		case PostgresErrorCodes.UNIQUE_VIOLATION:
22
			return uniqueViolation(error);
23
		case PostgresErrorCodes.NUMERIC_VALUE_OUT_OF_RANGE:
24
			return numericValueOutOfRange(error);
25
		case PostgresErrorCodes.VALUE_LIMIT_VIOLATION:
26
			return valueLimitViolation(error);
27
		case PostgresErrorCodes.NOT_NULL_VIOLATION:
28
			return notNullViolation(error);
29
		case PostgresErrorCodes.FOREIGN_KEY_VIOLATION:
30
			return foreignKeyViolation(error);
31
		default:
32
			return error;
33
	}
34
}
35

36
function uniqueViolation(error: PostgresError) {
37
	const { table, detail } = error;
38

39
	const betweenParens = /\(([^)]+)\)/g;
40
	const matches = detail.match(betweenParens);
41

42
	if (!matches) return error;
43

44
	const collection = table;
45
	const field = matches[0].slice(1, -1);
46

47
	return new RecordNotUniqueError({
48
		collection,
49
		field,
50
	});
51
}
52

53
function numericValueOutOfRange(error: PostgresError) {
54
	const regex = /"(.*?)"/g;
55
	const matches = error.message.match(regex);
56

57
	if (!matches) return error;
58

59
	const collection = matches[0].slice(1, -1);
60
	const field = null;
61

62
	return new ValueOutOfRangeError({
63
		collection,
64
		field,
65
	});
66
}
67

68
function valueLimitViolation(error: PostgresError) {
69
	/**
70
	 * NOTE:
71
	 * Postgres doesn't return the offending column
72
	 */
73

74
	const regex = /"(.*?)"/g;
75
	const matches = error.message.match(regex);
76

77
	if (!matches) return error;
78

79
	const collection = matches[0].slice(1, -1);
80
	const field = null;
81

82
	return new ValueTooLongError({
83
		collection,
84
		field,
85
	});
86
}
87

88
function notNullViolation(error: PostgresError) {
89
	const { table, column } = error;
90
	if (!column) return error;
91

92
	if (error.message.endsWith('contains null values')) {
93
		return new ContainsNullValuesError({ collection: table, field: column });
94
	}
95

96
	return new NotNullViolationError({
97
		collection: table,
98
		field: column,
99
	});
100
}
101

102
function foreignKeyViolation(error: PostgresError) {
103
	const { table, detail } = error;
104

105
	const betweenParens = /\(([^)]+)\)/g;
106
	const matches = detail.match(betweenParens);
107

108
	if (!matches) return error;
109

110
	const collection = table;
111
	const field = matches[0].slice(1, -1);
112

113
	return new InvalidForeignKeyError({
114
		collection,
115
		field,
116
	});
117
}
118

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.