Skip to content

Commit

Permalink
Merge pull request #4 from andgar2010/3.3.0
Browse files Browse the repository at this point in the history
Enhance HttpStatus Class with Improved Error Handling and Immutability
  • Loading branch information
tech-andgar authored Feb 24, 2024
2 parents 2b24534 + 182cfd3 commit 8b971ee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [3.3.0] - 2024-02-24

### Added - 3.3.0

- Added @immutable annotation to HttpStatus class for enhanced immutability.

### Fixed - 3.3.0

- Improved error messaging for unrecognized fromCode() of HTTP status codes and added verification test case.

## [3.2.0] - 2024-02-17

### Fixed - 3.2.0
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

Constants enumerating the HTTP status codes in Dart. All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and RFC2518 (WebDAV) are supported.

## Codes
<details>
<summary><h2>Codes of Http Status </h2></summary>

| Code | Http Status Name | Http Status (v1.x - v2.x Deprecated) | Http Status (v2.x - v3.x) |
| ---- | ---------------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------- |
Expand Down Expand Up @@ -87,6 +88,8 @@ A library for debugging and displaying http status codes.
Includes 63 status codes, messages and desciptions sourced from
the official spec <https://tools.ietf.org/html/rfc723> and <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/>

</details>

## Installation

```shell
Expand Down
9 changes: 8 additions & 1 deletion lib/src/http_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:meta/meta.dart';

import 'http_status_code.dart';
import 'utils/int_http_status_code_extension.dart';

Expand Down Expand Up @@ -74,6 +76,7 @@ import 'utils/int_http_status_code_extension.dart';
/// User agents __SHOULD__ display any included entity to the user.
/// These response codes are applicable to any request method.
///
@immutable
class HttpStatus {
HttpStatus({
required this.code,
Expand All @@ -97,7 +100,11 @@ class HttpStatus {

factory HttpStatus.fromCode(int code) {
if (!_httpStatusCodes.containsKey(code)) {
throw ArgumentError.value(code, 'code', 'Unknown status code');
throw ArgumentError.value(
code,
'code',
'Unrecognized status code. Use the HttpStatus constructor for custom codes',
);
}

return _httpStatusCodes[code]!;
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: http_status
description: Constants enumerating the HTTP status codes in Dart. All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and RFC2518 (WebDAV) are supported.
version: 3.2.0
version: 3.3.0
repository: https://github.com/DartForge/http_status
issue_tracker: https://github.com/DartForge/http_status/issues
homepage: https://github.com/DartForge/http_status
Expand All @@ -13,6 +13,9 @@ topics: [http-status]
environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
meta: ^1.11.0

dev_dependencies:
coverage: ^1.7.2
dart_code_linter: ^1.1.2
Expand Down
22 changes: 22 additions & 0 deletions test/src/http_status_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,28 @@ void main() {
expect(() => HttpStatus.fromCode(998), throwsArgumentError);
expect(() => HttpStatus.fromCode(1), throwsArgumentError);
});
test('Throws ArgumentError on unrecognized status code', () {
expect(
() => HttpStatus.fromCode(600),
throwsA(
isA<ArgumentError>().having(
(e) => e.message,
'message',
contains(
'Unrecognized status code. Use the HttpStatus constructor for custom codes',
),
),
),
);
final HttpStatus httpStatus600 = HttpStatus(
code: 600,
name: 'name custom 600',
description: 'description custom 600',
);
expect(httpStatus600.code, 600);
expect(httpStatus600.name, 'name custom 600');
expect(httpStatus600.description, 'description custom 600');
});

test('fromCode constructor returns correct status', () {
expect(
Expand Down

0 comments on commit 8b971ee

Please sign in to comment.