Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- 8
- 10
install:
- npm install
script:
- npm build
- npm test
498 changes: 498 additions & 0 deletions lib/PostgresError.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PostgresError } from './PostgresError';
82 changes: 82 additions & 0 deletions lib/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { join } from 'path';
import { writeFileSync } from 'fs';
import lodashFlatten from 'lodash.flatten';
import isomorphicFetch from 'isomorphic-fetch';

const sourceUrl = (branch = 'master') =>
`https://github.com/postgres/postgres/raw/${branch}/src/backend/utils/errcodes.txt`;

const getSourceText = (): Promise<string[]> =>
isomorphicFetch(sourceUrl())
.then(response => response.text())
.then(text => text.split('\n'));

const stripCommentsAndEmptyLines = (lines: string[]) =>
lines.filter(line => line !== '' && line.charAt(0) !== '#');

const sectionRegex = /^Section:\s(?<description>.*)$/;

const groupBySections = (lines: string[]) => {
const sections: Record<string, { description: string, lines: string[] }> = {};
let currentSection = '';

lines.forEach(line => {
const matches = line.match(sectionRegex);

if (matches) {
currentSection = (matches as any).groups.description;
sections[currentSection] = {
description: currentSection,
lines: [],
};
} else {
sections[currentSection].lines.push(line);
}
});

return Object.values(sections);
}

const errorLineRegex =
/^(?<sqlstate>[A-Z0-9]*)\s*(?<severity>[EWS])\s*ERRCODE_(?<constant>[A-Z_]*)\s*(?<code>[a-z_]*)$/

const parseErrorLine = (line: string) => {
const matches = line.match(errorLineRegex);

if (!matches) {
throw new Error(`Error parsing error line:\n\t"${line}"`);
}

const { sqlstate, severity, constant, code } = (matches as any).groups;

return { sqlstate, severity, constant, code };
};

const parseSectionLines = (data: ReturnType<typeof groupBySections>) =>
data.map(({ lines, ...otherData }) => ({ errorCodes: lines.map(parseErrorLine), ...otherData }));

const getEnum = async () => {
const errorSections =
await getSourceText()
.then(stripCommentsAndEmptyLines)
.then(groupBySections)
.then(parseSectionLines);

const enumEntries = errorSections.map(section =>
section.errorCodes.map(
errorCode => ` /** ${section.description}: [${errorCode.severity}] ${errorCode.code} */\n ${errorCode.constant} = '${errorCode.sqlstate}',`,
),
);

const allEntries = lodashFlatten(enumEntries);

return `export enum PostgresError {\n${
allEntries.join('\n')
}\n}\n`;
}

const writeEnum = (enumString: string) => {
writeFileSync(join(__dirname, '../lib/PostgresError.ts'), enumString);
}

getEnum().then(writeEnum);
Loading