2015-10-13 14:31:29 -07:00
|
|
|
// Copyright (c) .NET Foundation. All rights reserved.
|
|
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
|
|
|
|
using System;
|
2015-10-15 15:09:37 -07:00
|
|
|
using Microsoft.Extensions.JsonParser.Sources;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
namespace Microsoft.Extensions.ProjectModel
|
|
|
|
{
|
|
|
|
public sealed class FileFormatException : Exception
|
|
|
|
{
|
|
|
|
private FileFormatException(string message) :
|
|
|
|
base(message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private FileFormatException(string message, Exception innerException) :
|
|
|
|
base(message, innerException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Path { get; private set; }
|
|
|
|
public int Line { get; private set; }
|
|
|
|
public int Column { get; private set; }
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return $"{Path}({Line},{Column}): Error: {base.ToString()}";
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static FileFormatException Create(Exception exception, string filePath)
|
|
|
|
{
|
2015-10-15 15:09:37 -07:00
|
|
|
if (exception is JsonDeserializerException)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
return new FileFormatException(exception.Message, exception)
|
|
|
|
.WithFilePath(filePath)
|
2015-10-15 15:09:37 -07:00
|
|
|
.WithLineInfo((JsonDeserializerException)exception);
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new FileFormatException(exception.Message, exception)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(Exception exception, JsonValue jsonValue, string filePath)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = Create(exception, jsonValue)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(Exception exception, JsonValue jsonValue)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = new FileFormatException(exception.Message, exception)
|
|
|
|
.WithLineInfo(jsonValue);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(string message, JsonValue jsonValue, string filePath)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = Create(message, jsonValue)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static FileFormatException Create(string message, string filePath)
|
|
|
|
{
|
|
|
|
var result = new FileFormatException(message)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(string message, JsonValue jsonValue)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = new FileFormatException(message)
|
|
|
|
.WithLineInfo(jsonValue);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal FileFormatException WithFilePath(string path)
|
|
|
|
{
|
|
|
|
if (path == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
Path = path;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
private FileFormatException WithLineInfo(JsonValue value)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
if (value == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(value));
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
Line = value.Line;
|
|
|
|
Column = value.Column;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
private FileFormatException WithLineInfo(JsonDeserializerException exception)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
if (exception == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(exception));
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
Line = exception.Line;
|
|
|
|
Column = exception.Column;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 15:09:37 -07:00
|
|
|
}
|