Skip to content

Commit

Permalink
refactor(store,world-modules): clean up error names/locations (#2334)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Feb 28, 2024
1 parent 462c9ec commit 970aa10
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 153 deletions.
11 changes: 11 additions & 0 deletions .changeset/empty-camels-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@latticexyz/store": major
"@latticexyz/world-modules": minor
---

Moved custom errors out of libraries and interfaces to the file level. In most cases, these errors are contained inside the same file as the library that uses them.

Some have also been renamed:

- `FieldLayoutLib_*` errors to `FieldLayout_*`
- `SchemaLib_*` errors to `Schema_*`
154 changes: 84 additions & 70 deletions docs/pages/store/reference/misc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,62 @@ function slice32(bytes memory data, uint256 start) internal pure returns (bytes3
| -------- | --------- | -------------------------------------------------------------------------- |
| `output` | `bytes32` | The extracted bytes32 value from the specified position in the bytes blob. |

## FieldLayout_Empty

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)

```solidity
error FieldLayout_Empty();
```

## FieldLayout_InvalidStaticDataLength

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)

```solidity
error FieldLayout_InvalidStaticDataLength(uint256 staticDataLength, uint256 computedStaticDataLength);
```

## FieldLayout_StaticLengthDoesNotFitInAWord

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)

```solidity
error FieldLayout_StaticLengthDoesNotFitInAWord(uint256 index);
```

## FieldLayout_StaticLengthIsNotZero

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)

```solidity
error FieldLayout_StaticLengthIsNotZero(uint256 index);
```

## FieldLayout_StaticLengthIsZero

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)

```solidity
error FieldLayout_StaticLengthIsZero(uint256 index);
```

## FieldLayout_TooManyDynamicFields

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)

```solidity
error FieldLayout_TooManyDynamicFields(uint256 numFields, uint256 maxFields);
```

## FieldLayout_TooManyFields

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)

```solidity
error FieldLayout_TooManyFields(uint256 numFields, uint256 maxFields);
```

## FieldLayoutInstance

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)
Expand Down Expand Up @@ -470,50 +526,6 @@ function encode(uint256[] memory _staticFieldLengths, uint256 numDynamicFields)
| -------- | ------------- | ------------------------------------------------------------ |
| `<none>` | `FieldLayout` | A FieldLayout structure containing the encoded field layout. |

### Errors

#### FieldLayoutLib_TooManyFields

```solidity
error FieldLayoutLib_TooManyFields(uint256 numFields, uint256 maxFields);
```

#### FieldLayoutLib_TooManyDynamicFields

```solidity
error FieldLayoutLib_TooManyDynamicFields(uint256 numFields, uint256 maxFields);
```

#### FieldLayoutLib_Empty

```solidity
error FieldLayoutLib_Empty();
```

#### FieldLayoutLib_InvalidStaticDataLength

```solidity
error FieldLayoutLib_InvalidStaticDataLength(uint256 staticDataLength, uint256 computedStaticDataLength);
```

#### FieldLayoutLib_StaticLengthIsZero

```solidity
error FieldLayoutLib_StaticLengthIsZero(uint256 index);
```

#### FieldLayoutLib_StaticLengthIsNotZero

```solidity
error FieldLayoutLib_StaticLengthIsNotZero(uint256 index);
```

#### FieldLayoutLib_StaticLengthDoesNotFitInAWord

```solidity
error FieldLayoutLib_StaticLengthDoesNotFitInAWord(uint256 index);
```

## FieldLayout

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/FieldLayout.sol)
Expand Down Expand Up @@ -799,6 +811,26 @@ type and a name_
type ResourceId is bytes32;
```

## Schema_InvalidLength

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Schema.sol)

_Error raised when the provided schema has an invalid length._

```solidity
error Schema_InvalidLength(uint256 length);
```

## Schema_StaticTypeAfterDynamicType

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Schema.sol)

_Error raised when a static type is placed after a dynamic type in a schema._

```solidity
error Schema_StaticTypeAfterDynamicType();
```

## SchemaInstance

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Schema.sol)
Expand Down Expand Up @@ -991,24 +1023,6 @@ function encode(SchemaType[] memory schemas) internal pure returns (Schema);
| -------- | -------- | ------------------- |
| `<none>` | `Schema` | The encoded Schema. |

### Errors

#### SchemaLib_InvalidLength

_Error raised when the provided schema has an invalid length._

```solidity
error SchemaLib_InvalidLength(uint256 length);
```

#### SchemaLib_StaticTypeAfterDynamicType

_Error raised when a static type is placed after a dynamic type in a schema._

```solidity
error SchemaLib_StaticTypeAfterDynamicType();
```

## Schema

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Schema.sol)
Expand All @@ -1023,6 +1037,14 @@ _Defines and handles the encoding/decoding of Schemas which describe the layout
type Schema is bytes32;
```

## Slice_OutOfBounds

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Slice.sol)

```solidity
error Slice_OutOfBounds(bytes data, uint256 start, uint256 end);
```

## SliceInstance

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Slice.sol)
Expand Down Expand Up @@ -1192,14 +1214,6 @@ function getSubslice(bytes memory data, uint256 start, uint256 end) internal pur
| -------- | ------- | ------------------------------------- |
| `<none>` | `Slice` | A new Slice representing the subslice |

### Errors

#### Slice_OutOfBounds

```solidity
error Slice_OutOfBounds(bytes data, uint256 start, uint256 end);
```

## Slice

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Slice.sol)
Expand Down
10 changes: 0 additions & 10 deletions docs/pages/store/reference/store-hook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,3 @@ function onAfterDeleteRecord(ResourceId tableId, bytes32[] memory keyTuple, Fiel
| `tableId` | `ResourceId` | The ID of the table where the record was deleted. |
| `keyTuple` | `bytes32[]` | An array representing the composite key for the record. |
| `fieldLayout` | `FieldLayout` | The layout of the field, see FieldLayout.sol. |

### Errors

#### StoreHook_NotImplemented

Error emitted when a function is not implemented.

```solidity
error StoreHook_NotImplemented();
```
10 changes: 10 additions & 0 deletions docs/pages/store/reference/store.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ event Store_DeleteRecord(ResourceId indexed tableId, bytes32[] keyTuple);
| `tableId` | `ResourceId` | The ID of the table where the record is deleted. |
| `keyTuple` | `bytes32[]` | An array representing the composite key for the record. |

## StoreHook_NotImplemented

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/errors.sol)

Error emitted when a function is not implemented.

```solidity
error StoreHook_NotImplemented();
```

## Store_IndexOutOfBounds

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/errors.sol)
Expand Down
38 changes: 19 additions & 19 deletions packages/store/src/FieldLayout.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ type FieldLayout is bytes32;
// When importing FieldLayout, attach FieldLayoutInstance to it
using FieldLayoutInstance for FieldLayout global;

error FieldLayout_TooManyFields(uint256 numFields, uint256 maxFields);
error FieldLayout_TooManyDynamicFields(uint256 numFields, uint256 maxFields);
error FieldLayout_Empty();
error FieldLayout_InvalidStaticDataLength(uint256 staticDataLength, uint256 computedStaticDataLength);
error FieldLayout_StaticLengthIsZero(uint256 index);
error FieldLayout_StaticLengthIsNotZero(uint256 index);
error FieldLayout_StaticLengthDoesNotFitInAWord(uint256 index);

/**
* @title FieldLayoutLib
* @dev A library for handling field layout encoding into a single bytes32.
* It provides a function to encode static and dynamic fields and ensure
* various constraints regarding the length and size of the fields.
*/
library FieldLayoutLib {
error FieldLayoutLib_TooManyFields(uint256 numFields, uint256 maxFields);
error FieldLayoutLib_TooManyDynamicFields(uint256 numFields, uint256 maxFields);
error FieldLayoutLib_Empty();
error FieldLayoutLib_InvalidStaticDataLength(uint256 staticDataLength, uint256 computedStaticDataLength);
error FieldLayoutLib_StaticLengthIsZero(uint256 index);
error FieldLayoutLib_StaticLengthIsNotZero(uint256 index);
error FieldLayoutLib_StaticLengthDoesNotFitInAWord(uint256 index);

/**
* @notice Encodes the given field layout into a single bytes32.
* @dev Ensures various constraints on the length and size of the fields.
Expand All @@ -45,17 +45,17 @@ library FieldLayoutLib {
uint256 fieldLayout;
uint256 totalLength;
uint256 totalFields = _staticFieldLengths.length + numDynamicFields;
if (totalFields > MAX_TOTAL_FIELDS) revert FieldLayoutLib_TooManyFields(totalFields, MAX_TOTAL_FIELDS);
if (totalFields > MAX_TOTAL_FIELDS) revert FieldLayout_TooManyFields(totalFields, MAX_TOTAL_FIELDS);
if (numDynamicFields > MAX_DYNAMIC_FIELDS)
revert FieldLayoutLib_TooManyDynamicFields(numDynamicFields, MAX_DYNAMIC_FIELDS);
revert FieldLayout_TooManyDynamicFields(numDynamicFields, MAX_DYNAMIC_FIELDS);

// Compute the total static length and store the field lengths in the encoded fieldLayout
for (uint256 i; i < _staticFieldLengths.length; ) {
uint256 staticByteLength = _staticFieldLengths[i];
if (staticByteLength == 0) {
revert FieldLayoutLib_StaticLengthIsZero(i);
revert FieldLayout_StaticLengthIsZero(i);
} else if (staticByteLength > WORD_SIZE) {
revert FieldLayoutLib_StaticLengthDoesNotFitInAWord(i);
revert FieldLayout_StaticLengthDoesNotFitInAWord(i);
}

unchecked {
Expand Down Expand Up @@ -153,28 +153,28 @@ library FieldLayoutInstance {
*/
function validate(FieldLayout fieldLayout) internal pure {
if (fieldLayout.isEmpty()) {
revert FieldLayoutLib.FieldLayoutLib_Empty();
revert FieldLayout_Empty();
}

uint256 _numDynamicFields = fieldLayout.numDynamicFields();
if (_numDynamicFields > MAX_DYNAMIC_FIELDS) {
revert FieldLayoutLib.FieldLayoutLib_TooManyDynamicFields(_numDynamicFields, MAX_DYNAMIC_FIELDS);
revert FieldLayout_TooManyDynamicFields(_numDynamicFields, MAX_DYNAMIC_FIELDS);
}

uint256 _numStaticFields = fieldLayout.numStaticFields();
uint256 _numTotalFields = _numStaticFields + _numDynamicFields;
if (_numTotalFields > MAX_TOTAL_FIELDS) {
revert FieldLayoutLib.FieldLayoutLib_TooManyFields(_numTotalFields, MAX_TOTAL_FIELDS);
revert FieldLayout_TooManyFields(_numTotalFields, MAX_TOTAL_FIELDS);
}

// Static lengths must be valid
uint256 _staticDataLength;
for (uint256 i; i < _numStaticFields; ) {
uint256 staticByteLength = fieldLayout.atIndex(i);
if (staticByteLength == 0) {
revert FieldLayoutLib.FieldLayoutLib_StaticLengthIsZero(i);
revert FieldLayout_StaticLengthIsZero(i);
} else if (staticByteLength > WORD_SIZE) {
revert FieldLayoutLib.FieldLayoutLib_StaticLengthDoesNotFitInAWord(i);
revert FieldLayout_StaticLengthDoesNotFitInAWord(i);
}
_staticDataLength += staticByteLength;
unchecked {
Expand All @@ -183,13 +183,13 @@ library FieldLayoutInstance {
}
// Static length sums must match
if (_staticDataLength != fieldLayout.staticDataLength()) {
revert FieldLayoutLib.FieldLayoutLib_InvalidStaticDataLength(fieldLayout.staticDataLength(), _staticDataLength);
revert FieldLayout_InvalidStaticDataLength(fieldLayout.staticDataLength(), _staticDataLength);
}
// Unused fields must be zero
for (uint256 i = _numStaticFields; i < MAX_TOTAL_FIELDS; i++) {
uint256 staticByteLength = fieldLayout.atIndex(i);
if (staticByteLength != 0) {
revert FieldLayoutLib.FieldLayoutLib_StaticLengthIsNotZero(i);
revert FieldLayout_StaticLengthIsNotZero(i);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions packages/store/src/IStoreHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { PackedCounter } from "./PackedCounter.sol";
import { ResourceId } from "./ResourceId.sol";

interface IStoreHook is IERC165 {
/// @notice Error emitted when a function is not implemented.
error StoreHook_NotImplemented();

/**
* @notice Called before setting a record in the store.
* @param tableId The ID of the table where the record is to be set.
Expand Down
4 changes: 2 additions & 2 deletions packages/store/src/PackedCounter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ uint256 constant VAL_BITS = 5 * BYTE_TO_BITS;
// Maximum value of a 5-byte section
uint256 constant MAX_VAL = type(uint40).max;

error PackedCounter_InvalidLength(uint256 length);

/**
* @title PackedCounter Library
* @notice Static functions for handling PackedCounter type.
Expand Down Expand Up @@ -136,8 +138,6 @@ library PackedCounterLib {
* @dev Offers decoding, extracting, and setting functionalities for a PackedCounter.
*/
library PackedCounterInstance {
error PackedCounter_InvalidLength(uint256 length);

/**
* @notice Decode the accumulated counter from a PackedCounter.
* @dev Extracts the right-most 7 bytes of a PackedCounter.
Expand Down
Loading

0 comments on commit 970aa10

Please sign in to comment.