Move PosixError into puter-js-common

The in-progress git client also needs to use this. puter-js-common uses
commonjs modules, so it had to be adjusted for that.
This commit is contained in:
Sam Atkins 2024-05-16 10:11:20 +01:00
parent 1f6a2093fb
commit e43b21387c
6 changed files with 18 additions and 11 deletions

View File

@ -20,7 +20,7 @@ import fs from 'fs';
import path_ from 'path'; import path_ from 'path';
import modeString from 'fs-mode-to-string'; import modeString from 'fs-mode-to-string';
import { ErrorCodes, PosixError } from '../PosixError.js'; import { ErrorCodes, PosixError } from '@heyputer/puter-js-common/src/PosixError.js';
function convertNodeError(e) { function convertNodeError(e) {
switch (e.code) { switch (e.code) {

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { ErrorCodes, PosixError } from '../PosixError.js'; import { ErrorCodes, PosixError } from '@heyputer/puter-js-common/src/PosixError.js';
function convertPuterError(e) { function convertPuterError(e) {
// Handle Puter SDK errors // Handle Puter SDK errors

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { ErrorCodes, ErrorMetadata, errorFromIntegerCode } from '../../platform/PosixError.js'; import { ErrorCodes, ErrorMetadata, errorFromIntegerCode } from '@heyputer/puter-js-common/src/PosixError.js';
import { Exit } from './coreutil_lib/exit.js'; import { Exit } from './coreutil_lib/exit.js';
const maxErrorNameLength = Object.keys(ErrorCodes) const maxErrorNameLength = Object.keys(ErrorCodes)

View File

@ -18,7 +18,7 @@
*/ */
import { Exit } from './coreutil_lib/exit.js'; import { Exit } from './coreutil_lib/exit.js';
import { resolveRelativePath } from '../../util/path.js'; import { resolveRelativePath } from '../../util/path.js';
import { ErrorCodes } from '../../platform/PosixError.js'; import { ErrorCodes } from '@heyputer/puter-js-common/src/PosixError.js';
export default { export default {
name: 'touch', name: 'touch',

View File

@ -19,7 +19,7 @@
import assert from 'assert'; import assert from 'assert';
import { MakeTestContext } from './harness.js' import { MakeTestContext } from './harness.js'
import builtins from '../../src/puter-shell/coreutils/__exports__.js'; import builtins from '../../src/puter-shell/coreutils/__exports__.js';
import { ErrorCodes, ErrorMetadata } from '../../src/platform/PosixError.js'; import { ErrorCodes, ErrorMetadata } from '@heyputer/puter-js-common/src/PosixError.js';
export const runErrnoTests = () => { export const runErrnoTests = () => {
describe('errno', function () { describe('errno', function () {

View File

@ -1,9 +1,9 @@
/* /*
* Copyright (C) 2024 Puter Technologies Inc. * Copyright (C) 2024 Puter Technologies Inc.
* *
* This file is part of Phoenix Shell. * This file is part of Puter.
* *
* Phoenix Shell is free software: you can redistribute it and/or modify * Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published * it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or * by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
export const ErrorCodes = { const ErrorCodes = {
EACCES: Symbol.for('EACCES'), EACCES: Symbol.for('EACCES'),
EADDRINUSE: Symbol.for('EADDRINUSE'), EADDRINUSE: Symbol.for('EADDRINUSE'),
ECONNREFUSED: Symbol.for('ECONNREFUSED'), ECONNREFUSED: Symbol.for('ECONNREFUSED'),
@ -37,7 +37,7 @@ export const ErrorCodes = {
}; };
// Codes taken from `errno` on Linux. // Codes taken from `errno` on Linux.
export const ErrorMetadata = new Map([ const ErrorMetadata = new Map([
[ErrorCodes.EPERM, { code: 1, description: 'Operation not permitted' }], [ErrorCodes.EPERM, { code: 1, description: 'Operation not permitted' }],
[ErrorCodes.ENOENT, { code: 2, description: 'File or directory not found' }], [ErrorCodes.ENOENT, { code: 2, description: 'File or directory not found' }],
[ErrorCodes.EIO, { code: 5, description: 'IO error' }], [ErrorCodes.EIO, { code: 5, description: 'IO error' }],
@ -57,7 +57,7 @@ export const ErrorMetadata = new Map([
[ErrorCodes.ECONNREFUSED, { code: 111, description: 'Connection refused' }], [ErrorCodes.ECONNREFUSED, { code: 111, description: 'Connection refused' }],
]); ]);
export const errorFromIntegerCode = (code) => { const errorFromIntegerCode = (code) => {
for (const [errorCode, metadata] of ErrorMetadata) { for (const [errorCode, metadata] of ErrorMetadata) {
if (metadata.code === code) { if (metadata.code === code) {
return errorCode; return errorCode;
@ -66,7 +66,7 @@ export const errorFromIntegerCode = (code) => {
return undefined; return undefined;
}; };
export class PosixError extends Error { class PosixError extends Error {
// posixErrorCode can be either a string, or one of the ErrorCodes above. // posixErrorCode can be either a string, or one of the ErrorCodes above.
// If message is undefined, a default message will be used. // If message is undefined, a default message will be used.
constructor(posixErrorCode, message) { constructor(posixErrorCode, message) {
@ -141,3 +141,10 @@ export class PosixError extends Error {
return new PosixError(ErrorCodes.ETIMEDOUT, message); return new PosixError(ErrorCodes.ETIMEDOUT, message);
} }
} }
module.exports = {
ErrorCodes,
ErrorMetadata,
errorFromIntegerCode,
PosixError,
}