diff --git a/src/Stratis.Bitcoin.Features.Api/NodeController.cs b/src/Stratis.Bitcoin.Features.Api/NodeController.cs
index 596d11db50..17019e21c6 100644
--- a/src/Stratis.Bitcoin.Features.Api/NodeController.cs
+++ b/src/Stratis.Bitcoin.Features.Api/NodeController.cs
@@ -220,33 +220,34 @@ public IActionResult Status([FromQuery] bool publish)
/// The hash of the block to retrieve.
/// A flag that specifies whether to return the block header in the JSON format. Defaults to true. A value of false is currently not supported.
/// Json formatted . null if block not found. Returns formatted error if fails.
- /// Thrown if isJsonFormat = false"
- /// Thrown if hash is empty.
- /// Thrown if logger is not provided.
+ /// Returns the blockheader if found.
+ /// Null hash provided, BlockHeader does not exist or if isJsonFormat = false>/response>
/// Binary serialization is not supported with this method.
[Route("getblockheader")]
[HttpGet]
+ [ProducesResponseType((int)HttpStatusCode.OK)]
+ [ProducesResponseType((int)HttpStatusCode.BadRequest)]
+ [ProducesResponseType((int)HttpStatusCode.NotFound)]
public IActionResult GetBlockHeader([FromQuery] string hash, bool isJsonFormat = true)
{
try
{
- Guard.NotEmpty(hash, nameof(hash));
+ if (string.IsNullOrEmpty(hash))
+ return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, "Error", "Null hash provided.");
this.logger.LogDebug("GetBlockHeader {0}", hash);
+
if (!isJsonFormat)
{
this.logger.LogError("Binary serialization is not supported.");
- throw new NotImplementedException();
+ return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, "Error", "Binary serialization is not supported.");
}
- BlockHeaderModel model = null;
BlockHeader blockHeader = this.chainIndexer?.GetHeader(uint256.Parse(hash))?.Header;
- if (blockHeader != null)
- {
- model = new BlockHeaderModel(blockHeader);
- }
+ if (blockHeader == null)
+ return this.NotFound($"Block header for '{hash}' not found");
- return this.Json(model);
+ return this.Json(new BlockHeaderModel(blockHeader));
}
catch (Exception e)
{
diff --git a/src/Stratis.Bitcoin.Features.BlockStore.Tests/BlockStoreControllerTests.cs b/src/Stratis.Bitcoin.Features.BlockStore.Tests/BlockStoreControllerTests.cs
index 612f5cd55a..55730a8d4b 100644
--- a/src/Stratis.Bitcoin.Features.BlockStore.Tests/BlockStoreControllerTests.cs
+++ b/src/Stratis.Bitcoin.Features.BlockStore.Tests/BlockStoreControllerTests.cs
@@ -68,9 +68,9 @@ public void Get_Block_When_Hash_Is_Not_Found_Should_Return_OkResult_WithMessage(
IActionResult response = controller.GetBlock(new SearchByHashRequest() { Hash = new uint256(1).ToString(), OutputJson = true });
- response.Should().BeOfType();
- var result = (OkObjectResult)response;
- result.StatusCode.Should().Be((int)HttpStatusCode.OK);
+ response.Should().BeOfType();
+ var result = (NotFoundObjectResult)response;
+ result.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
result.Value.Should().Be("Block not found");
}
diff --git a/src/Stratis.Bitcoin.Features.BlockStore/Controllers/BlockStoreController.cs b/src/Stratis.Bitcoin.Features.BlockStore/Controllers/BlockStoreController.cs
index d7c5500ef9..a413d28f13 100644
--- a/src/Stratis.Bitcoin.Features.BlockStore/Controllers/BlockStoreController.cs
+++ b/src/Stratis.Bitcoin.Features.BlockStore/Controllers/BlockStoreController.cs
@@ -122,6 +122,7 @@ public IActionResult GetAddressIndexerTip()
[HttpGet]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
+ [ProducesResponseType((int)HttpStatusCode.NotFound)]
public IActionResult GetBlock([FromQuery] SearchByHashRequest query)
{
if (!this.ModelState.IsValid)
@@ -134,14 +135,14 @@ public IActionResult GetBlock([FromQuery] SearchByHashRequest query)
ChainedHeader chainedHeader = this.chainIndexer.GetHeader(blockId);
if (chainedHeader == null)
- return this.Ok("Block not found");
+ return this.NotFound("Block not found");
Block block = chainedHeader.Block ?? this.blockStore.GetBlock(blockId);
// In rare occasions a block that is found in the
// indexer may not have been pushed to the store yet.
if (block == null)
- return this.Ok("Block not found");
+ return this.NotFound("Block not found");
if (!query.OutputJson)
{
diff --git a/src/Stratis.Bitcoin.Tests/Controllers/NodeControllerTest.cs b/src/Stratis.Bitcoin.Tests/Controllers/NodeControllerTest.cs
index df7a3dba37..8b6c538072 100644
--- a/src/Stratis.Bitcoin.Tests/Controllers/NodeControllerTest.cs
+++ b/src/Stratis.Bitcoin.Tests/Controllers/NodeControllerTest.cs
@@ -526,7 +526,7 @@ public void GetBlockHeader_NotUsingJsonFormat_ThrowsNotImplementedException()
Assert.Single(errorResponse.Errors);
ErrorModel error = errorResponse.Errors[0];
Assert.Equal(400, error.Status);
- Assert.StartsWith("System.NotImplementedException", error.Description);
+ Assert.StartsWith("Binary serialization is not", error.Description);
}
[Fact]
@@ -555,10 +555,7 @@ public void GetBlockHeader_BlockHeaderNotFound_ReturnsNull()
string hash = new uint256(2562).ToString();
bool isJsonFormat = true;
- var json = (JsonResult)this.controller.GetBlockHeader(hash, isJsonFormat);
- var resultModel = (BlockHeaderModel)json.Value;
-
- Assert.Null(resultModel);
+ Assert.IsType(this.controller.GetBlockHeader(hash, isJsonFormat));
}
[Fact]